我正在處理VS13和SQL Server 2012中的項目,並且遇到了一些小問題。我似乎無法從我的數據庫中讀取數據。每當我嘗試對用於讀取數據的SqlDataReader
對象執行任何操作時,它都會拋出異常。SqlDataReader對象在每次執行操作時都會拋出異常
我得到的例外是InvalidOperationException
。
看看我的代碼,我用SQL查詢作爲參數調用這個函數,並將返回的對象存儲在另一個SqlDataReader
對象中。
private SqlDataReader reader (string sqCommand)
{
myConnection.Open();
string string1;
string1 = sqCommand;
SqlDataReader a = null;
try
{
SqlCommand Newcommand = new SqlCommand(string1, myConnection);
a = Newcommand.ExecuteReader();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
myConnection.Close();
if (a.Read()) //**statement 1**
return a;
else
return null;
}
我得到在標記在上面和我的對象或接收到該對象執行每個操作的代碼的聲明1除外。
任何人都可以告訴我什麼是我的代碼錯誤或提供任何其他解釋?如果需要更多其他代碼部分來查找錯誤,我可以提供。
感謝您的時間和您可能提供的幫助。 :)
看看這篇文章可能會對你有幫助。 http://stackoverflow.com/questions/23187029/sqldatareader-invalidoperationexception –