SQL异常在c#中未处理

时间:2010-09-11 14:18:13

标签: sql sql-server sqlexception

当我想调试此代码时,它会在objConnection.Open()上出错:

  

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

    SqlConnection objConnection = new SqlConnection(
"server=localhost;database=pubs;" +
"user id=sa;password=");
    SqlDataAdapter objDataAdapter = new SqlDataAdapter();
    DataSet objDataSet = new DataSet();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Set the SelectCommand properties... 
        objDataAdapter.SelectCommand = new SqlCommand();
        objDataAdapter.SelectCommand.Connection =
              objConnection;
        objDataAdapter.SelectCommand.CommandText =
         "SELECT au_lname, au_fname, title, price " +
         "FROM authors " +
        "JOIN titleauthor ON authors.au_id = " +
        "titleauthor.au_id " +
         "JOIN titles ON titleauthor.title_id = " +
        "titles.title_id " +
         "ORDER BY au_lname, au_fname";
        objDataAdapter.SelectCommand.CommandType =
             CommandType.Text;
        // Open the database connection... 
        **objConnection.Open();**
        // Fill the DataSet object with data... 
        objDataAdapter.Fill(objDataSet, "authors");
        // Close the database connection... 
        objConnection.Close();
        // Set the DataGridView properties  
        // to bind it to our data... 
        grdAuthorTitles.AutoGenerateColumns = true;
        grdAuthorTitles.DataSource = objDataSet;
        grdAuthorTitles.DataMember = "authors";
        // Clean up 
        objDataAdapter = null;
        objConnection = null; 
    }

2 个答案:

答案 0 :(得分:3)

当您的服务器名称错误或sql server未启用时,通常会发生此错误。

localhost and (local) are treated differently for sql server。您可能想尝试(本地)。

以下是connection strings的列表,可以帮助您解决问题。

如果您正在使用sql express,那么您可能希望将其从localhost更改为。\ sqlexpress。

要检查您的SQL Server是否已启用,请确保其服务已启用。您可以在服务中以及Configuration Manager中执行此操作。

答案 1 :(得分:1)

首先,使用SqlDataAdapter,您无需专门打开和关闭SqlConnection - 适配器将为您执行此操作。

其次,我强烈建议将所有ADO.NET代码放入using(.....) { .... }块作为最佳实践。

此外,在本地PC上,通常不需要为数据库指定特定用户,但可以在SQL连接字符串中直接使用内置Windows身份验证(integrated security=SSPI)。

最后一点:如果您的DataSet中不需要多个表格,那么使用DataTable会更容易也更好 - 开销更少,性能损失更少。无需将表名指定为数据成员等。简单易懂。

试试这段代码:

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    using(SqlConnection objConnection = new SqlConnection("server=(local);database=pubs;integrated security=SSPI;"))
    {
        SqlDataAdapter objDataAdapter = new SqlDataAdapter();

        // Set the SelectCommand properties... 
        objDataAdapter.SelectCommand = new SqlCommand();
        objDataAdapter.SelectCommand.Connection = objConnection;
        objDataAdapter.SelectCommand.CommandText =
            "SELECT au_lname, au_fname, title, price FROM authors " +
            "JOIN titleauthor ON authors.au_id = titleauthor.au_id " +
            "JOIN titles ON titleauthor.title_id = titles.title_id " +
            "ORDER BY au_lname, au_fname";

        DataTable tblData = new DataTable();

        // Fill the DataSet object with data... 
        objDataAdapter.Fill(tblData);

        // Set the DataGridView properties  
        // to bind it to our data... 
        grdAuthorTitles.AutoGenerateColumns = true;
        grdAuthorTitles.DataSource = tblData;
   }
}

这样,将自动为您处理引用的类。