2012-10-22 65 views
-2
SqlConnection cnn = new SqlConnection(); 
cnn.ConnectionString = "Data Source=.;Database = deptStore;Integrated Security = true;"; 

cnn.Open(); 
SqlCommand cmd = new SqlCommand(); 
cmd.CommandText = "insert into Employee values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "')"; 
cmd.Connection = cnn; 

cmd.ExecuteNonQuery(); 

Response.Write("Record Save"); 
cnn.Close(); 

連接,但我收到以下錯誤:試圖建立與SQL Server 2008

SqlException was unhandled by user code

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

請幫助我理解的錯誤並糾正它。

+0

我投票結束這個問題 - 很多人給出瞭解決方案,你所做的只是說「不工作」。你必須參與並閱讀答案並回答他們的答案。 **我們將無法解決這個問題如果您所做的只是複製一個連接字符串和報告它不起作用** – Hogan

回答

0

您是否驗證過SQL Server配置正確?您可以通過打開SQL Server配置工具來完成此操作。

如果要連接到SQL Server Express,則需要指定連接字符串中始終爲SQLExpress的實例的名稱。

Data Source=.\SQLEXPRESS"... 
+0

它將通過deFault Data Source =。\ SQLEXPRESS ... – user1716251

+0

N靜止不工作.. ... – user1716251

0

首先檢查你的webconfig。應該有一個這樣的代碼

<connectionStrings> 
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=deptStore;Integrated Security=True;" providerName="System.Data.SqlClient" /> 

或者如果你想只需更換YourDataBaseName。 接下來是設置您的連接

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnStringDb1"].ToString()); 

而且多數民衆贊成它,你有一個連接字符串

您需要了解更多關於這個緣故,我也看到你的插入查詢是錯誤的

試This

cnn.ConnectionString =「Data Source = localhost; Database = deStStore; Integrated Security = true;」;

+0

不工作Sir ... – user1716251

+0

@ user1716251你知道如何建立連接嗎? – Androyds