急!!c#ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。
publicvoidShowBook(){lvBook.Clear();lvBook.Columns.Clear();lvBook.Columns.Add("",0);l...
public void ShowBook()
{
lvBook.Clear();
lvBook.Columns.Clear();
lvBook.Columns.Add("", 0);
lvBook.Columns.Add("书名", 220);
lvBook.Columns.Add("作者", 120);
lvBook.Columns.Add("价格", 60);
lvBook.Columns.Add("出版社", 300);
lvBook.Columns.Add("ISBN", 120);
conn = new SqlConnection(strConn);
sqlCmd = conn.CreateCommand();
sqlCmd.CommandText = "select bookid, bookname, author, press, price, ISBN from book";
sqlReader = sqlCmd.ExecuteReader();
conn.Open();
while (sqlReader.Read())
{
ListViewItem newlvi = new ListViewItem(sqlReader["bookid"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["bookname"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["author"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["price"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["press"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["ISBN"].ToString().Trim());
lvBook.Items.Add(newlvi);
}
sqlReader.Close();
conn.Close();
lvBook.Items[0].Selected = true;
} 展开
{
lvBook.Clear();
lvBook.Columns.Clear();
lvBook.Columns.Add("", 0);
lvBook.Columns.Add("书名", 220);
lvBook.Columns.Add("作者", 120);
lvBook.Columns.Add("价格", 60);
lvBook.Columns.Add("出版社", 300);
lvBook.Columns.Add("ISBN", 120);
conn = new SqlConnection(strConn);
sqlCmd = conn.CreateCommand();
sqlCmd.CommandText = "select bookid, bookname, author, press, price, ISBN from book";
sqlReader = sqlCmd.ExecuteReader();
conn.Open();
while (sqlReader.Read())
{
ListViewItem newlvi = new ListViewItem(sqlReader["bookid"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["bookname"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["author"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["price"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["press"].ToString().Trim());
newlvi.SubItems.Add(sqlReader["ISBN"].ToString().Trim());
lvBook.Items.Add(newlvi);
}
sqlReader.Close();
conn.Close();
lvBook.Items[0].Selected = true;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询