2012-06-23 39 views
-1

我只想循環訪問數據庫中的結果。這就是我想要做的所有事情!這是我的錯誤,當我嘗試調試:與SQL Server建立連接時發生網絡相關或特定於實例的錯誤

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

它指出,這條線:

// fill dataset into named table 
myAdapter.Fill(myDataSet, "Sent"); 

這裏是我的全部代碼:

// get connection strings from app.config 
    string conString = ConfigurationManager.ConnectionStrings["Database1ConnectionString"].ConnectionString; 

    // connection object 
    SqlConnection myConn = new SqlConnection(conString); 

    // create adapter 
    SqlDataAdapter myAdapter = new SqlDataAdapter("select * from `Sent`", myConn); 

    // create command builder 
    SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter); 

    // create dataset 
    DataSet myDataSet = new DataSet(); 

    // fill dataset into named table 
    myAdapter.Fill(myDataSet, "Sent"); 

    // query using linq 
    IEnumerable<DataRow> results 
     = from row in myDataSet.Tables["Sent"].AsEnumerable() 
      select row; 

    // enumerate through results 
    foreach (DataRow row in results) 
    { 
     MessageBox.Show(row.ToString()); 
    } 

我能做些什麼解決這個問題?另外,如果任何人在C#中使用數據庫更簡單,我會接受你的想法,因爲這對我來說很荒謬(在C#中操作數據庫)。感謝您的幫助。

更新12年6月29日:

下面的代碼對我的作品:

 // get connection strings from app.config 
     string conString = ConfigurationManager.ConnectionStrings["Database1ConnectionString"].ConnectionString; 

     // connection object 
     SqlCeConnection myConn = new SqlCeConnection(conString); 

     // create adapter 
     SqlCeDataAdapter myAdapter = new SqlCeDataAdapter("select * from [Sent]", myConn); 

     // create command builder 
     SqlCeCommandBuilder myCommandBuilder = new SqlCeCommandBuilder(myAdapter); 

     // create dataset 
     DataSet myDataSet = new DataSet(); 

     // fill dataset into named table 
     myAdapter.Fill(myDataSet, "Sent"); 

     // create new row 
     DataRow newRow = myDataSet.Tables["Sent"].NewRow(); 

     // set field vals 
     newRow["sendTo"] = sendTo; 
     newRow["subject"] = subject; 
     newRow["message"] = message; 

     // add new row to table 
     myDataSet.Tables["Sent"].Rows.Add(newRow); 

     // update database 
     myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand(); 
     int updatedRows = myAdapter.Update(myDataSet, "Sent"); 
     //MessageBox.Show("There were "+updatedRows.ToString()+" updated rows"); 

     // close connection 
     myConn.Close(); 
+1

您需要檢查的第一件事是您的連接字符串是否正確以及它是否指向正確的服務器/數據庫。 –

+10

第一步:閱讀信息並採取行動。第二步:谷歌的消息。 – usr

+0

我同意@usr。錯誤信息是非常明顯的,即使不是由於某種原因,谷歌幾秒鐘就能很快找到答案。問題出在你的連接字符串或SQL服務器本身。這個問題不屬於這裏。在這裏問一個問題之前,預計你至少要做一些粗略的研究。 – dodexahedron

回答

0

我沒有使用正確的代碼(它沒有任何與任何連接東西做)。這是我第一次試圖用PHP以外的語言連接到數據庫。現在,我可能會使用LINQ或Lambda表達式而不是此代碼。但這裏是我使用哪些工作:

// get connection strings from app.config 
    string conString = ConfigurationManager.ConnectionStrings["Database1ConnectionString"].ConnectionString; 

    // connection object 
    SqlCeConnection myConn = new SqlCeConnection(conString); 

    // create adapter 
    SqlCeDataAdapter myAdapter = new SqlCeDataAdapter("select * from [Sent]", myConn); 

    // create command builder 
    SqlCeCommandBuilder myCommandBuilder = new SqlCeCommandBuilder(myAdapter); 

    // create dataset 
    DataSet myDataSet = new DataSet(); 

    // fill dataset into named table 
    myAdapter.Fill(myDataSet, "Sent"); 

    // create new row 
    DataRow newRow = myDataSet.Tables["Sent"].NewRow(); 

    // set field vals 
    newRow["sendTo"] = sendTo; 
    newRow["subject"] = subject; 
    newRow["message"] = message; 

    // add new row to table 
    myDataSet.Tables["Sent"].Rows.Add(newRow); 

    // update database 
    myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand(); 
    int updatedRows = myAdapter.Update(myDataSet, "Sent"); 
    //MessageBox.Show("There were "+updatedRows.ToString()+" updated rows"); 

    // close connection 
    myConn.Close(); 
1

首先:檢查你的連接字符串錯誤直接告訴你「嘿,我無法連接到您的數據庫「

第二個:select * from Sent(不帶撇號)

+0

與錯誤消息無關,但您對此正確。 – usr

+0

我只是想連接到本地數據庫。這裏是我的app.config: 我不知道什麼是正確的連接字符串,也許你可以告訴我。我的數據庫與C:\ Documents and Settings \ Administrator \ My Documents \ Visual Studio 2010 \ Projects \ TestingApp \ TestingApp \ Database1.sdf中的項目(也位於\ bin文件夾中)位於同一目錄中。 – user1477388

+0

我用正確的代碼更新了問題。 – user1477388

1

您是否更改了您在安裝SQL服務器的機器中使用的密碼?我以前遇到過這種情況,而我所做的是將我的新密碼反映到SQL Server配置中,然後重新啓動服務。

另請確保在SQL配置設置中啓用了TCP/IP設置。

相關問題