2012-08-11 57 views
0

從C#連接到SQL Server 2008 R2,我有一個嚴重的問題,因爲我已經改變了一些SQL Server設置。現在,我可以不通過下面的代碼連接到SQL Server:SQL插入不工作c#querybuilder

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     kamiDataSetTableAdapters.kami2TableAdapter kap = new  WindowsFormsApplication1.kamiDataSetTableAdapters.kami2TableAdapter(); 

     decimal a = Convert.ToDecimal("11.2"); 
     int b = 11; 

     kap.Insert(Convert.ToDecimal("11.2"), b, Convert.ToDecimal("11.2"), Convert.ToDecimal("11.2")); 
    } 
} 

}

(表中有四個小數和一個INT列)

但是我可以連接並插入或QueryBuilder的選擇字段GUI。不幸的是,由於名譽低下,我無法上傳圖片。

和錯誤是

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

請你指導我,我的老闆很生氣!

預先感謝您。

+1

連接字符串在哪裏?您確定SQL Server正在運行並配置爲接受遠程連接嗎?該錯誤消息似乎很清楚什麼是錯的... – 2012-08-11 15:58:59

+0

聽起來像SQL Server實例不允許遠程連接,這將是我會檢查的第一件事。看看你是否可以Telnet到端口1433上的SQL Server實例。 – 2012-08-11 16:05:39

+0

是啊sql正在運行,因爲我可以通過querybuilder連接並執行命令。 – 2012-08-11 20:18:39

回答

0

不知道你改變了什麼設置,除了連接問題之外很難說清楚。你需要從應用程序的連接字符串開始;因爲它聽起來像你可以使用不同的連接。

我假設遠程連接可能不是它;因爲你可以連接。

如果您更改了身份驗證設置或數據庫權限,則需要確保您的應用程序連接的任何帳戶都具有適當的權限。

+0

我真的不記得我操縱了哪個設置。我剛剛關注了一些主題,並使用該命令啓用了SQL身份驗證。 – 2012-08-11 20:11:08

+0

你能詳細解釋一下你提到的正確許可嗎? – 2012-08-11 20:13:01

+0

我想我有權連接到sql,因爲querybuilder(defalut gui)連接並正確執行。但問題是適配器。 – 2012-08-11 20:16:09

0

兩種可能性

1 /如果你使用SQL身份驗證: 做出的SqlConnection,並嘗試打開與您的用戶名和密碼 的連接,如果它適用於您的ConnectionString更改用戶密碼

如果您使用Windows身份驗證: 您設置的連接字符串Trusted_Connection,並檢查是否正常工作

2 /確保指定好SQL Server實例(服務器\ serverinstance)

+0

1)我使用sqlconnection來完成它,但「conn.open();」遇到相同的錯誤消息。 2)我用點來指定實例,我的意思是同一臺服務器。這很重要嗎? – 2012-08-11 20:08:45

+0

它不是用點而是斜線(windows認證):Server = myServerName \ theInstanceName; Database = myDataBase; Trusted_Connection = True; – 2012-08-12 00:31:47