在做数据库的查询过程中,使用方法ExecuteReader,其返回结果为MySqlDataReader,由于参考的信息有误,走了好长时间的弯路,记录下来;
string connectionStr = "server=localhost;uid=root;password=;database=db_family";
string sqlContent = "select * from t_user";
MySqlConnection sqlConnection = null;
MySqlDataReader reader = null;
try
{
sqlConnection = new MySqlConnection(connectionStr);
sqlConnection.Open();
MySqlCommand command = new MySqlCommand(sqlContent,sqlConnection);
reader = command.ExecuteReader();
while (reader.Read())
{
if (reader.HasRows)
{
Console.WriteLine(reader.GetInt32(0) + ":" + reader.GetString(1) + ":" + reader.GetString(2));
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
finally
{
reader.Close();
sqlConnection.Close();
}

本文详细介绍了在数据库查询中使用ExecuteReader方法的过程,包括连接数据库、执行SQL语句和读取结果集的具体实现。同时,记录了作者在实践中遇到的问题及解决弯路的经验。

3160

被折叠的 条评论
为什么被折叠?



