错误:System.Data.dll中出现类型为“System.Data.SqlClient.SqlException”的未处理异常

时间:2021-08-27 13:21:15

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

System.Data.dll中发生了未处理的“System.Data.SqlClient.SqlException”类型异常

Additional information: 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)

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

I used these codes:

我用这些代码:

public partial class Form1 : Form
{
    SqlConnection con = new SqlConnection();
    public Form1()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";

        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'sTUDENTDataSet.login' table. You can move, or remove it, as needed.  
        //this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);  
        SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True");
        con.Open();

        {
        }
    }

    private void btnLogin_Click_1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";
        con.Open();
        string UserId = txtUsername.Text;
        string UserPass = txtPassword.Text;
        SqlCommand cmd = new SqlCommand("Select UserId,UserPass from Login where UserId='" + txtUsername.Text + "'and UserPass='" + txtPassword.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            MessageBox.Show("Login sucess!");
            Form2 form = new Form2();
            form.Show();
        }
        else
        {
            MessageBox.Show("Invalid Login Information. Please check username and password");
        }
        con.Close();
    }

The error here is the con.Open(); that belongs here:

这里的错误是con.Open();属于这里:

SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";
            con.Open();

I tried removing it because I don't know what else to do and the second error is on da.Fill(dt); So I guess the only problem that should really be fixed is the con.Open();

我尝试删除它,因为我不知道还能做什么,第二个错误是在da.Fill(dt);所以我想唯一应该解决的问题是con.Open();

What should I do?

我该怎么办?

2 个解决方案

#1


1  

The error is in your connection string.

错误在您的连接字符串中。

As a DataSource you have to specify SERVER\INSTANCE; SQLEXPRESS is usually an instance name in default installation, so try:

作为DataSource,您必须指定SERVER \ INSTANCE; SQLEXPRESS通常是默认安装中的实例名称,因此请尝试:

con.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";

., (LOCAL), LOCALHOST and YourMachineName are all equivalent, referring to your own machine as a server. If your database is on another PC you have to specify its name.

。,(LOCAL),LOCALHOST和YourMachineName都是等效的,将您自己的机器称为服务器。如果您的数据库位于另一台PC上,则必须指定其名称。

#2


0  

Your connection string looks incomplete. While it names a server (SQLEXPRESS), it omits any mention of which data base.

连接字符串看起来不完整。虽然它命名服务器(SQLEXPRESS),但它忽略了对哪个数据库的提及。

Although it refers instead to LocalDB, perhaps comparing the working connection string below against yours will suggest to you what you need to add.

虽然它引用的是LocalDB,但是将下面的工作连接字符串与您的工作连接字符串进行比较可能会向您建议您需要添加的内容。

Data Source=(LocalDB)\v11.0;AttachDbFilename="$$WorkingDirectory$$\RGUNC_Tag_Browser\RGUNC_Tags.mdf";Integrated Security=True

数据源=(LocalDB)\ v11.0; AttachDbFilename =“$$ WorkingDirectory $$ \ RGUNC_Tag_Browser \ RGUNC_Tags.mdf”; Integrated Security = True

The bottom line is that the error message is telling you that it cannot locate your data base with the information supplied in your connection string.

底线是错误消息告诉您它无法使用连接字符串中提供的信息找到您的数据库。

#1


1  

The error is in your connection string.

错误在您的连接字符串中。

As a DataSource you have to specify SERVER\INSTANCE; SQLEXPRESS is usually an instance name in default installation, so try:

作为DataSource,您必须指定SERVER \ INSTANCE; SQLEXPRESS通常是默认安装中的实例名称,因此请尝试:

con.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";

., (LOCAL), LOCALHOST and YourMachineName are all equivalent, referring to your own machine as a server. If your database is on another PC you have to specify its name.

。,(LOCAL),LOCALHOST和YourMachineName都是等效的,将您自己的机器称为服务器。如果您的数据库位于另一台PC上,则必须指定其名称。

#2


0  

Your connection string looks incomplete. While it names a server (SQLEXPRESS), it omits any mention of which data base.

连接字符串看起来不完整。虽然它命名服务器(SQLEXPRESS),但它忽略了对哪个数据库的提及。

Although it refers instead to LocalDB, perhaps comparing the working connection string below against yours will suggest to you what you need to add.

虽然它引用的是LocalDB,但是将下面的工作连接字符串与您的工作连接字符串进行比较可能会向您建议您需要添加的内容。

Data Source=(LocalDB)\v11.0;AttachDbFilename="$$WorkingDirectory$$\RGUNC_Tag_Browser\RGUNC_Tags.mdf";Integrated Security=True

数据源=(LocalDB)\ v11.0; AttachDbFilename =“$$ WorkingDirectory $$ \ RGUNC_Tag_Browser \ RGUNC_Tags.mdf”; Integrated Security = True

The bottom line is that the error message is telling you that it cannot locate your data base with the information supplied in your connection string.

底线是错误消息告诉您它无法使用连接字符串中提供的信息找到您的数据库。