ExecuteReader要求已打开且可用的链接。连接的当前状态为已关闭
在IIS下浏览自己开发的网站,出现上述问题。在VS2008下调试没有任何问题。
错误显示如下:
ExecuteReader要求已打开且可用的链接。连接的当前状态为已关闭
源错误:
70行 : m_DBComm.CommandText = "select CMaster_Pwd from T_ClassMaster where CMaster_ID = " + userID;
71行 : SqlDataReader reader = m_DBComm.ExecuteReader();
72行 :
73行 : reader.Read();
我的程序是一个 用户登录模块,给出代码:
单例模式设计数据库,类文件如下:
public class DBAccess
{
private SqlConnection m_DBConn;
private SqlCommand m_DBComm;
private static DBAccess m_DBaccess = null;
private DBAccess()
{
m_DBConn = new SqlConnection();
m_DBComm = new SqlCommand();
if (!InitialDB())
{
// MessageBox.Show("数据库初始化失败!");
}
}
public static DBAccess GetDBInstans()
{
if (null == m_DBaccess)
{
m_DBaccess = new DBAccess();
}
return m_DBaccess;
}
public bool InitialDB()
{
m_DBConn.ConnectionString = "Initial Catalog = Educational Administration System; Data Source = localhost;Integrated Security = SSPI";
try
{
m_DBComm.Connection = m_DBConn;
m_DBConn.Open(); //打开数据库
}
catch
{
return false;
}
return true;
}
public int CheckMasterPwd(string userID, string userPwd) // 用户登录检测
{
m_DBComm.CommandText = "select CMaster_Pwd from T_ClassMaster where CMaster_ID = " + userID;
SqlDataReader reader = m_DBComm.ExecuteReader();
reader.Read();
if (false == reader.HasRows)
{
reader.Close();
return -1;
}
string pass = reader["CMaster_Pwd"].ToString().Trim();
if (pass != userPwd)
{
reader.Close();
return 1;
}
else
{
reader.Close();
return 0;
}
}
}
//前台登录Button事件:
protected void Button1_Click(object sender, EventArgs e)
{
DBAccess.GetDBInstans().CheckMasterPwd(txtID.Text, txtPwd.Text);
int aaa = DBAccess.GetDBInstans().CheckMasterPwd(txtID.Text, txtPwd.Text);
if (0 == aaa)
Response.Write("<script>alert(\"正确登录\")</script>");
else
Response.Write("<script>alert(\"输入有误\")</script>");
}
错误显示如下:
ExecuteReader要求已打开且可用的链接。连接的当前状态为已关闭
源错误:
70行 : m_DBComm.CommandText = "select CMaster_Pwd from T_ClassMaster where CMaster_ID = " + userID;
71行 : SqlDataReader reader = m_DBComm.ExecuteReader();
72行 :
73行 : reader.Read();
我的程序是一个 用户登录模块,给出代码:
单例模式设计数据库,类文件如下:
public class DBAccess
{
private SqlConnection m_DBConn;
private SqlCommand m_DBComm;
private static DBAccess m_DBaccess = null;
private DBAccess()
{
m_DBConn = new SqlConnection();
m_DBComm = new SqlCommand();
if (!InitialDB())
{
// MessageBox.Show("数据库初始化失败!");
}
}
public static DBAccess GetDBInstans()
{
if (null == m_DBaccess)
{
m_DBaccess = new DBAccess();
}
return m_DBaccess;
}
public bool InitialDB()
{
m_DBConn.ConnectionString = "Initial Catalog = Educational Administration System; Data Source = localhost;Integrated Security = SSPI";
try
{
m_DBComm.Connection = m_DBConn;
m_DBConn.Open(); //打开数据库
}
catch
{
return false;
}
return true;
}
public int CheckMasterPwd(string userID, string userPwd) // 用户登录检测
{
m_DBComm.CommandText = "select CMaster_Pwd from T_ClassMaster where CMaster_ID = " + userID;
SqlDataReader reader = m_DBComm.ExecuteReader();
reader.Read();
if (false == reader.HasRows)
{
reader.Close();
return -1;
}
string pass = reader["CMaster_Pwd"].ToString().Trim();
if (pass != userPwd)
{
reader.Close();
return 1;
}
else
{
reader.Close();
return 0;
}
}
}
//前台登录Button事件:
protected void Button1_Click(object sender, EventArgs e)
{
DBAccess.GetDBInstans().CheckMasterPwd(txtID.Text, txtPwd.Text);
int aaa = DBAccess.GetDBInstans().CheckMasterPwd(txtID.Text, txtPwd.Text);
if (0 == aaa)
Response.Write("<script>alert(\"正确登录\")</script>");
else
Response.Write("<script>alert(\"输入有误\")</script>");
}
...全文
相关推荐
已有打开的与此命令相关联的DataReader,必须首先将它关闭。对于此异常的理解
今天与大家分享一下 已有打开的与此命令相关联的DataReader,必须首先将它关闭。这个异常的个人理解
C#使用ExecuteReader返回DataReader
C#使用ExecuteReader返回DataReader既有查询结果集又有输出参数和返回值的使用注意事项
c#中executereader执行查询示例分享
ExecuteReader执行查询实例ExecuteReader方法存在的目的只有一个:尽可能快地对数据库进行查询并得到结果。ExecuteReader 返回一个DataReader对象:如果在SqlCommand对象中调用,则返回SqlDataReader;如果在OleDbCommand对象中调用,返回的是OleDbDataReader。可以调用DataReader的方法和属性迭代处理结果集。它是一个快速枚举数据库查询结果的机制,是只读、只进的。对SqlDataReader.Read的每次调用都会从结果集中返回一行。 代码如下:using System;using System.Colle
数据库连接及关闭
C#和SQL2012的连接及关闭,参照课程自己写的小程序,适合新手参考!
sqlserver基础类(连接,关闭,查询,更新)
sqlserver基础类(连接,关闭,查询,更新)



