2012-10-22 34 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