我只想循環訪問數據庫中的結果。這就是我想要做的所有事情!這是我的錯誤,當我嘗試調試:與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();
您需要檢查的第一件事是您的連接字符串是否正確以及它是否指向正確的服務器/數據庫。 –
第一步:閱讀信息並採取行動。第二步:谷歌的消息。 – usr
我同意@usr。錯誤信息是非常明顯的,即使不是由於某種原因,谷歌幾秒鐘就能很快找到答案。問題出在你的連接字符串或SQL服務器本身。這個問題不屬於這裏。在這裏問一個問題之前,預計你至少要做一些粗略的研究。 – dodexahedron