当前位置: 代码迷 >> ASP.NET >> sqlhelper 疑问,该怎么处理
  详细解决方案

sqlhelper 疑问,该怎么处理

热度:5758   发布时间:2013-02-25 00:00:00.0
sqlhelper 疑问
写sqlhelper 有的人返回datatable 有的返回 datareader 有的返回dataset 返回那种类型最好呢

还有就是petshop里的

C# code
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;            }        }

这段代码里的这段注释做何解释(不是让翻译的啊)为什么他们不用using了呢
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

------解决方案--------------------------------------------------------
datatable和dataset一样,不过一般返回的是datatable, 省的再dataset.Tables[0]了.

datareader 差不多,想用哪个用哪个,

相对来说,datatable方便,datareader象征性的快了一点点点点点点
------解决方案--------------------------------------------------------
http://www.cnblogs.com/puke/archive/2007/07/24/829290
------解决方案--------------------------------------------------------
使用DataReader与DataSet都可以从数据源读取数据。DataReader本身是通过IDbCommand.ExecuteReader()方法进行构建的;而DataSet则是通过DbDataAdapter.Fill()方法进行填充。此外,两者的工作方式有明显的不同:DataReader的执行过程不能脱离数据库连接,也就是在DataReader读取数据的时候不能够使用IDbConnection.Close()方法关闭数据库连接;而在使用DataSet获取数据时,可以断开数据库的连接,因为此时DbDataAdapter已经负责将数据获取到应用服务器中了。
using语句,定义一个范围,在范围结束时处理对象