public static SqlDataReader dataReader(string sqlstr)
{
SqlDataReader dr=null;
try
{
SqlConnection conne=CreateCon();
conne.Open();
SqlCommand comme=new SqlCommand();
comme.Connection=conne;
comme.CommandText =sqlstr;
comme.CommandType =CommandType.Text ;
dr=comme.ExecuteReader(CommandBehavior.CloseConnection); 
}
catch//(Exception e)
{
try
{
dr.Close();

}
catch
{
}
//throw new Exception(e.Message);
}


               return dr;

}
这个类写的有问题吗?为什么网页用一段时间就出错打不开了呢?提示DATAREADER出错.

解决方案 »

  1.   

    DataReader读取数据时数据库连接必须是打开的,哪果关掉的话,肯定就会出错
      

  2.   

    那应该如何改呢?是在数据库访问的类里改呢?还是在调用DARAREADER的程序里改?
      

  3.   

    public static SqlConnection CreateCon()
    {
    return new SqlConnection( ConfigurationSettings.AppSettings["SqlConnectionStr"].ToString());

    有写...不是编译不过去的错..是用一段时间就出错了...
      

  4.   

    DataReader 调用完毕后没有关闭while ( dataReader.Read() )
    {
    }dataReader.Close();