DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection("Data Source=NSIC;Initial Catalog=Dev;User ID=sa;Password=123456"))
{
// SqlDataAdapter adapter = new SqlDataAdapter("Select * from Employee", connection);
connection.Open();
//adapter.Fill(ds);
using (SqlCommand command = new SqlCommand("Select * from Employee", connection))
{
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read()) //Error is here
{
var table = new DataTable();
table.Load(reader);
ds.Tables.Add(table);
}
}
}
}
GridView1.DataSource = ds;
我想從數據sql server.but檢索數據,但在while(Reader.read())
錯誤。無效嘗試呼叫讀取器關閉時讀取(Reader.read())。爲什麼?
無效嘗試呼叫讀取器關閉時讀取 while(Reader.read())。
爲什麼呢?
嘗試'ExecuteReader'沒有''CloseConnection' CommandBehavior'。像這樣'command.ExecuteReader()' – Irshad
你的讀者可能是空的。在while循環之前測試null。 – jdweng
爲什麼每行創建一個數據表?嘗試簡單地刪除while-part,留下3行。 –