简体   繁体   English

用户代码未处理的sqlexception

[英]sqlexception unhandled by user code

I experience this error when I try to load my web page. 尝试加载网页时,我遇到此错误。 Can anyone advise what cause this to happen and the remedy to it? 任何人都可以建议发生这种情况的原因和解决方法吗?

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server (提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接

This is the C# code 这是C#代码

string conStr = ConfigurationManager.ConnectionStrings["MSSQLConnectionString"].ConnectionString;

SqlConnection connection = new SqlConnection(conStr);

string strSqlCmd;
string strSqlCmd2;

strSqlCmd = "INSERT INTO User " + "(UserName, UserPwd, email, address, mobileNo, firstname, lastname, dateofBirth, NRIC, gender)";
strSqlCmd += " VALUES (@Username, @Userpwd, @email, @address, @mobileNo, @firstName, @lastName, @DateOfBirth, @nric, @gender)";

strSqlCmd2 = "INSERT INTO Patient " + "(Allergies, Childhoodillness)";
strSqlCmd2 += "VALUES(@allergies, @Childhood_illness)";

//create command and assign the query and connection from the constructor
SqlCommand cmd = new SqlCommand(strSqlCmd, connection);
SqlCommand cmd2 = new SqlCommand(strSqlCmd2, connection);

cmd.Parameters.AddWithValue("@Username", UserName.Text);
cmd.Parameters.AddWithValue("@Userpwd", Password.Text);
cmd.Parameters.AddWithValue("@email", Email.Text);
cmd.Parameters.AddWithValue("@address", Address.Text);
cmd.Parameters.AddWithValue("@mobileNo", MobileNo.Text);
cmd.Parameters.AddWithValue("@firstName", Password.Text);
cmd.Parameters.AddWithValue("@lastName", Password.Text);
cmd.Parameters.AddWithValue("@DateOfBirth", DOB.Text);
cmd.Parameters.AddWithValue("@nric", NRIC.Text);
cmd.Parameters.AddWithValue("@gender", Gender.Text);

cmd2.Parameters.AddWithValue("@allergies", Allergies.Text);
cmd2.Parameters.AddWithValue("@Childhood_illness", ChildhoodIllness.Text);

connection.Open();
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
connection.Close();

This is the web.config code 这是web.config代码

<add name="MSSQLConnectionString"
     connectionString="Server=localhost; Database= App_Data\PMS.mdf ; Integrated Security=True"
     providerName="System.Data.SqlClient" />

This error is self explanatory and it means that there is an error with your connection string. 此错误是不言自明的,它表示您的连接字符串有错误。 You may address wrong server name, or invalid database name. 您可能输入了错误的服务器名称或无效的数据库名称。 Also, if all these are OK, you may take a look at configuration manager and check to see if SQL Server is ready for accepting remote requests. 另外,如果所有这些都OK,您可以查看配置管理器,并检查SQL Server是否准备好接受远程请求。 (This is for remote connecting ONLY.) (这仅用于远程连接。)

You can get useful tips about connections strings in the following Url: http://www.connectionstrings.com/ 您可以在以下网址中获得有关连接字符串的有用提示: http : //www.connectionstrings.com/

This is incorrect connection string for local MDF database. 对于本地MDF数据库,这是不正确的连接字符串。 Try something like; 尝试类似的东西;

<add name="MSSQLConnectionString"
 connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|PMS.mdf;Integrated Security=True;"
 providerName="System.Data.SqlClient" />

Or 要么

<add name="MSSQLConnectionString"
 connectionString="Data Source=.\MSSQLSERVER;AttachDbFilename=|DataDirectory|PMS.mdf;Integrated Security=True;"
 providerName="System.Data.SqlClient" />

or any other instance that you have installed. 或您已安装的任何其他实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM