【问题标题】:Error: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll错误:System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常
【发布时间】:2015-07-30 15:13:11
【问题描述】:

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

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

我该怎么办?

【问题讨论】:

  • 如果您在 localhost 中运行此代码,请尝试将数据源更改为:localhost\SQLEXPRESS 并验证目录名称是否正确。
  • 看起来数据库在你的本地机器上。尝试使用 SQL Server Management Studio 和 Windows 身份验证连接到该数据库服务器。我觉得主机名不正确。如果正确,请检查所有 SQL Server 服务是否已启动并运行。
  • 'SqlConnection con = new SqlConnection(); con.ConnectionString = "本地主机=SQLEXPRESS;初始目录=StudentInformation;集成安全=True"; con.Open();'它说不支持本地主机
  • 请将 SQLEXPRESS 更改为点 (.) 或 localhost,然后反馈给我们

标签: c# sql-server visual-studio login sqlconnection


【解决方案1】:

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

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

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

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

【讨论】:

  • 我尝试了 .SQLEXPRESS 而不是 .\SQLEXPRESS 因为这将是一个错误,但是仍然是相同的错误,即 con.Open();
  • 请注意:错误在 con.Open() 中,但它指的是“未找到或无法访问”的服务器,并且服务器是通过 con.ConnectionString 指定的。
  • 您在哪里安装 SQL SERVER?它在你的电脑上吗?
  • 验证您的服务器/实例名称:stackoverflow.com/questions/141154/…
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多