DataReader不能使用using

来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=133
GPS平台、网站建设、软件开发,系统运维,找森大网络科技!

http://cnsendnet.taobao.com

public static MySqlDataReader ExecuteMySqlReader(string sqlStr)
{
MySqlConnection conn = new MySqlConnection(MyConString);
MySqlCommand cmd = new MySqlCommand(sqlStr, conn);
try
{
conn.Open();
//执行CloseConnection命令时,如果关闭关联的DataReader对象,则关联的Connection对象也将关闭
//用using(conn)会报错,因为执行完命令就会关闭连接,其它代码调用DataReader对象时,连接已经关闭。
MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}

GPS平台、网站建设、软件开发,系统运维,找森大网络科技!

http://cnsendnet.taobao.com

posted on 2019-12-02 17:53  森大科技  阅读(181)  评论(0编辑  收藏  举报

导航