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

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

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

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)

我使用了这些代码:

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

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

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

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

我该怎么办?

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

作为 DataSource 您必须指定 SERVER\INSTANCESQLEXPRESS 通常是默认安装的实例名称,所以尝试:

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

.(LOCAL)LOCALHOSTYourMachineName都是等价的,把自己的机器称为服务器。如果您的数据库在另一台 PC 上,则必须指定其名称。

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

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

数据源=(LocalDB)\v11.0;AttachDbFilename="$$WorkingDirectory$$\RGUNC_Tag_Browser\RGUNC_Tags.mdf";集成安全=True

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