2010-09-11 45 views
0

當我想調試代碼它給出objConnection.Open()錯誤:SQL例外的是在C#中未處理

sqlExeption was Unhandled and say(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))

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; 
    } 
+0

我明白,問題出在這個代碼:database = pubs因爲酒吧不存在它應該be.I有什麼我應該粘貼可用於此代碼的酒吧的酒吧? – Arash 2010-09-13 15:08:04

回答

3

通常,當您的服務器名稱錯誤或SQL Server出現此錯誤不在。

localhost and (local) are treated differently for sql server。你可能想嘗試(本地)。

下面是一個列表connection strings來幫助你。

如果您使用的是sql express,那麼您可能需要將其從localhost更改爲。\ sqlexpress。

要檢查您的SQL Server是否確定其服務已啓用。您可以在服務中以及在配置管理器中執行此操作。

+0

我想在我的電腦上使用它,是本地主機錯了嗎? – Arash 2010-09-11 14:24:55

+0

你能解釋我如何在sqlserver?我沒有那樣做 – Arash 2010-09-11 14:34:43

+0

打開它?打開您的管理工作室 - 選擇您的實例,右鍵單擊並選擇開始。您也可以進入服務並找到sql server服務並打開它並將其設置爲自動,如果您希望它始終在PC啓動時啓動。 – klabranche 2010-09-11 14:37:17

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

這樣,處置所引用的類將自動爲您處理。

+0

它給出了相同的錯誤:objDataAdapter.Fill(objDataSet,「authors」); – Arash 2010-09-11 14:52:26

+0

@arash:那麼我最好的猜測是:你的本地SQL Server確實沒有運行..... – 2010-09-11 14:54:17