双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

ExecuteReader要求已打开且可用的链接。连接的当前状态为已关闭

Greg_han Greg_han 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在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>");

}

...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
Greg_han
.NET社区 62,274 成员 668,968 内容