代码是从msdn里找的,做了些修改,代码如下:string queryString ="SELECT * FROM news;";using (SqlConnection connection =
new SqlConnection(sqlConn.ConnectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();SqlDataReader reader = command.ExecuteReader();// Call Read before accessing data.
while (reader.Read())
{
System.Windows.Forms.MessageBox.Show(reader[1].ToString());
}// Call Close when done reading.
reader.Close();
}
为什么在执行的过程中“SqlDataReader reader = command.ExecuteReader();”这句中出错呢?错误提示:
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
请赐教~~
new SqlConnection(sqlConn.ConnectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();SqlDataReader reader = command.ExecuteReader();// Call Read before accessing data.
while (reader.Read())
{
System.Windows.Forms.MessageBox.Show(reader[1].ToString());
}// Call Close when done reading.
reader.Close();
}
为什么在执行的过程中“SqlDataReader reader = command.ExecuteReader();”这句中出错呢?错误提示:
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
请赐教~~
应该是:string queryString ="SELECT * FROM news";
试试看
一个是:connection,一个是:sqlConn
sqlConn是在设计界面中添加的对象,并用它生成连接字符串,并测试连接成功~~new SqlConnection(sqlConn.ConnectionString))这里只是传给他参数~~查询语句改成没有分号的也不行啊~~是什么原因啊?
只是在reader中有问题~~我用command对象的executereader()也会报错~~~