问题描述
executereader要求已打开且可用的连接。连接的当前状态为已关闭。myConnection=newSqlConnection();myConnection.ConnectionString="server=localhost;uid=sa;pwd=sa;database=student";sqlCommand=newSqlCommand();sqlCommand.Connection=myConnection;sqlCommand.CommandType=CommandType.Text;sqlCommand.CommandText="selsect*fromstuinfo";myConnection.Open();SqlDataReaderdReader=sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);while(dReader.Read()){ListViewItemnewItem=listView1.Items.Add(dReader["sno"].ToString().Trim());newItem.SubItems.Add(dReader["sname"].ToString().Trim());newItem.SubItems.Add(dReader["sex"].ToString().Trim());newItem.SubItems.Add(dReader["depno"].ToString().Trim());newItem.SubItems.Add(dReader["birdate"].ToString().Trim());newItem.SubItems.Add(dReader["status"].ToString().Trim());newItem.SubItems.Add(dReader["postcode"].ToString().Trim());newItem.SubItems.Add(dReader["telenum"].ToString().Trim());newItem.SubItems.Add(dReader["address"].ToString().Trim());}dReader.Close();不知道有什么问题啊,要疯了~~
解决方案
解决方案二:
先myConnection.Open();然后再sqlCommand.Connection=myConnection;