/// <summary>
/// Execute a SqlCommand that returns a resultset against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="cmdType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param>
/// <param name="cmdText">存储过程的名字或者 T-SQL 语句</param>
/// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param>
/// <returns>A SqlDataReader containing the results</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
#region//ExecuteDataSet方法

  

================================================================================================

c#数据库访问返回值类型为SqlDataReader时使用using时注意的问题
2014-04-19      0个评论    来源:c#数据库访问返回值类型为SqlDataReader时使用using时注意的问题  
收藏    我要投稿

在封装通用 SQLSERVER 数据可访问方法时,如果返回值类型为 SqlDataReader ,那么在创建连接字符串的时候,我们不能写成如下

public static SqlDataReader ExecuteReader(string strSQL)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

using (SqlCommand cmd = new SqlCommand(strSQL, connection))

{

try

{

connection.Open();

SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

return myReader;

}

catch (System.Data.SqlClient.SqlException ex)

{

throw new Exception(ex.Message);

}

}

}

}

你在使用using创建的时候,在SqlDataReader 赋值后return时,SqlConnection 就会被释放资源,连接就会被关闭。而我们传递过去的SqlDataReader 是引用类型,接收传递过去的SqlDataReader 的地方调用的时候,就会提示连接已经被关闭,无法调用,因为 using (SqlConnection connection = new SqlConnection(connectionString))在方法结束时,就把资源释放了,并关闭了连接,为了正常接收传递过去的SqlDataReader ,在创建连接的时候不能用using,正确的写法如下

public static SqlDataReader ExecuteReader(string strSQL)

{

SqlConnection connection = new SqlConnection(connectionString);

using (SqlCommand cmd = new SqlCommand(strSQL, connection))

{

try

{

connection.Open();

SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

return myReader;

}

catch (System.Data.SqlClient.SqlException ex)

{

throw new Exception(ex.Message);

}

}

}

加红字段:CommandBehavior.CloseConnection有何作用:

1、CommandBehavior.CloseConnection有何作用

2、CommandBehavior.CloseConnection的作用

3、ExecuteReader方法中CommandBehavior.CloseConnection的一些注意事项

C# / MSSQL / WinForm / ASP.NET - SQLHelper中返回SqlDataReader数据的更多相关文章

  1. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

  2. JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  3. 【jmeter】JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  4. JMeter中返回Json数据的处理方法(转)

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  5. jmeter(二十)JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  6. Spring MVC中返回JSON数据的几种方式

    我们都知道Spring MVC 的Controller方法中默认可以返回ModeAndView 和String 类型,返回的这两种类型数据是被DispatcherServlet拿来给到视图解析器进行继 ...

  7. ASP.NET MVC5中View-Controller间数据的传递

    使用ASP.NET MVC做开发时,经常需要在页面(View)和控制器(Controller)之间传递数据,那么都有哪些数据传递的方式呢? 本文对于View向Controller中传值共列举了以下几种 ...

  8. JS获取子窗口中返回的数据

    在开发的时候,遇到了这样一个问题,客户填写自己的收货地址,可以新建,但同时也可以选择之前填写的,由于我们的客户本身就是商户,地址繁多,把它之前的地址简单用个下拉框罗列出来显然不合适,并且客户要求能够对 ...

  9. MVC中返回json数据的两种方式

    MVC里面如果直接将数据返回到前端页面,我们常用的方式就是用return view(): 那么我不想直接用razor语法,毕竟razor这玩意儿实在是太难记了,还不如写ajax对接来得舒服不是 那么我 ...

随机推荐

  1. 高级爬虫工程师(Spider)-美团网-拉勾网-最专业的互联网招聘平台

    高级爬虫工程师(Spider)-美团网-拉勾网-最专业的互联网招聘平台 高级爬虫工程师(Spider)

  2. HDU 2087 剪花布条 KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2087 KMP匹配数—— AC代码: #include <iostream> #includ ...

  3. Android:源码环境编译自定义的APP到ROM(System Image)中

    有时候我们需要在源码环境中增加自己的应用或模块,随ROM一起发布. 下面讲述相关步骤: 1. 首先可以在SDK环境下进行编码设计(如果你的APP不涉及到emulator无法模拟的硬件的话) 也可以参考 ...

  4. iOS 开发http post 文件的上传

    iOS开发网络篇—文件的上传 说明:文件上传使用的时POST请求,通常把要上传的数据保存在请求体中.本文介绍如何不借助第三方框架实现iOS开发中得文件上传. 由于过程较为复杂,因此本文只贴出部分关键代 ...

  5. NSnotificationCenter 正确使用姿势, removeObject 探索

    最近在做平板的过程中,发现了一些很不规范的代码.偶然修复支付bug的时候,看到其他项目代码,使用通知的地方没有移除,我以为我这个模块的支付闪退是因为他通知没有移除的缘故.而在debug和看了具体的代码 ...

  6. iOS UISearchBar UISearchController

    搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...

  7. 如何设置gen_server在退出时执行相关操作

    如果gen_server在监控树中不需要stop函数,gen_server会由其supervisor根据shutdown策略自动终止掉.如果要在进程终止之前执行清理,shutdown策略必须设定一个t ...

  8. C语言深度剖析---const关键字(转载)

    const是constant的缩写,是恒定不变的意思.被const修饰的值,是只读变量. 1.const修饰只读变量,具有不变性      #include <stdio.h> int m ...

  9. 分享一个Redis帮助类

    最近在项目中使用了redis来存储已经下载过的URL,项目中用的是ServiceStack来操作Redis,一开始ServiceStack的版本用的是最新的,后来发现ServiceStack已经商业化 ...

  10. 基于visual Studio2013解决C语言竞赛题之0411公约数和公倍数

       题目 解决代码及点评 求最大公约数和最小公倍数,方法已经在题目中有提示,分析代码实现如下: /* 题目: 输入两个正整数 m和 n,求其最大公约数和最小公倍数. */ #includ ...