-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();

但我收到以下错误:

SqlException 未被用户代码处理

建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

请帮助我理解错误并纠正它。

4

2 回答 2

0

您是否验证 SQL Server 配置正确?您可以通过打开 SQL Server 配置工具来执行此操作。

如果要连接到 SQL Server Express,则需要指定始终SQLExpress在连接字符串中的实例名称。

IE

Data Source=.\SQLEXPRESS"...
于 2012-10-22T08:21:46.233 回答
0

首先检查您的网络配置。应该有这样的代码

<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());

就是这样,你有一个连接字符串

你需要阅读更多关于这个原因我也看到你的插入查询是错误的

尝试这个

cnn.ConnectionString = "数据源=localhost;数据库=deptStore;集成安全=true;";

于 2012-10-22T08:23:19.427 回答