首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

阅读器关闭时尝试调用 Read 无效。解决思路

2012-04-05 
阅读器关闭时尝试调用 Read 无效。急!数据库类,using Systemusing System.Datausing System.Configuratio

阅读器关闭时尝试调用 Read 无效。急!
数据库类,
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.OleDb;
//using System.Data.SqlClient;

/// <summary>
///WebData 的摘要说明
/// </summary>
public class WebData
{

public WebData()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
  

  /// <summary> 
  /// 执行Sql查询语句 
  /// </summary> 
  /// <param name="sqlstr">传入的Sql语句</param> 
  /// 执行SQL语句,返回影响的记录数
  /// </summary>
  /// <param name="SQLString">SQL语句</param>
  /// <returns>影响的记录数</returns>
  public static int ExecuteSql(string sqlstr)
  {
  using(OleDbConnection conn=new OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["webtest"].ConnectionString))
  {
  using (OleDbCommand cmd = new OleDbCommand(sqlstr, conn))
  {
  try
  {
  conn.Open();
  int rows = cmd.ExecuteNonQuery();
  return rows;
  }
  catch (System.Data.OleDb.OleDbException e)
  {
  conn.Close();
  throw e;
  }
  finally
  {
  cmd.Dispose();
  conn.Close();
  }
  }
  }
  }


  /// <summary> 
  /// 执行Sql更新语句 
  /// </summary> 
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <returns>布尔值</returns>
  public static int ExecuteUpdate(string sqlstr)
  {
  using(OleDbConnection conn=new OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["webtest"].ConnectionString))
  {
  using (OleDbCommand cmd = new OleDbCommand(sqlstr, conn))
  {
  try
  {
  conn.Open();
  int rows = cmd.ExecuteNonQuery();
  return rows;
  }
  catch (System.Data.OleDb.OleDbException e)
  {
  conn.Close();
  throw e;
  }
  finally
  {
  cmd.Dispose();
  conn.Close();
  }
  }
  }

  }



/// <summary>
/// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
/// 方法关闭数据库连接
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>SqlDataReader对象</returns>
public static OleDbDataReader dataReader(string sqlstr)
{
OleDbDataReader dr=null;

OleDbConnection conn = new OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["webtest"].ConnectionString);


OleDbCommand cmd = new OleDbCommand(sqlstr, conn);

try
{
conn.Open();
dr = cmd.ExecuteReader();
return dr;
}
catch (System.Data.OleDb.OleDbException e)
{
conn.Close();
throw e;
}
finally
{

cmd.Dispose();
conn.Close();
}


}
   

  ///<summary>
  ///关闭datareader
  ///传入SqlDataReader的ref
  ///</summary>
  //public static void closeDataReader(ref OleDbDataReader dr)
  //{
  // try
  // {
  // dr.Close();
  // dr.Dispose();
  // }
  // catch (System.Data.OleDb.OleDbException e)
  // {
  // dr.Close();
  // dr.Dispose();
  // }
  //}
后台代码:
  [AjaxPro.AjaxMethod]
  public string returns(string s1,string s2,string s3)
  {
   
  string ma = Session["CheckCode"].ToString().ToLower();
  string EnPswdStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s2, "MD5");

  string sqls = "SELECT User_name, User_PassWord FROM Hover_Admin WHERE User_name = '" + s1 + "' AND User_PassWord = '" + EnPswdStr.Trim() + "'";
   
  OleDbDataReader red = WebData.dataReader(sqls);

  if (red.Read())
  {
  if (s3.ToLower() != ma)
  {
  return s3 = "dd";
  }
  mycookie.SaveCookie("UserName", s1, 2);
  mycookie.SaveCookieC("Online", "True|" + s1, 2);
  return s1 = "true";
  //return s1 = red["User_name"].ToString();

  //red.Close();
  //red.Dispose();
  //WebData.closeDataReader(ref red);

  }
  else
  {
  return s2 = "false";
  }
   
  }
阅读器关闭时尝试调用 Read 无效[b][/b]


[解决办法]

C# code
public static OleDbDataReader dataReader(string sqlstr)     {         OleDbDataReader dr=null;         OleDbConnection conn = new OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["webtest"].ConnectionString);         OleDbCommand cmd = new OleDbCommand(sqlstr, conn);                             try                 {                     conn.Open();                     dr =  cmd.ExecuteReader(CommandBehavior.CloseConnection);                    return dr;                 }                 catch                 {                    if (dr != null)                   {                    dr.Close();                   }                   throw;                 }               } 

热点排行