错误提示:ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭。
源代码如下:请问是什么原因啊?
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.Form["userName"].ToString();
string userPwd = Request.Form["userPwd"].ToString(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'";
SqlCommand cmd1 = new SqlCommand(s,con);
try
{ int count = Convert.ToInt32(cmd1.ExecuteScalar());
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
源代码如下:请问是什么原因啊?
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.Form["userName"].ToString();
string userPwd = Request.Form["userPwd"].ToString(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'";
SqlCommand cmd1 = new SqlCommand(s,con);
try
{ int count = Convert.ToInt32(cmd1.ExecuteScalar());
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
{
string userName = Request.Form["userName"].ToString();
string userPwd = Request.Form["userPwd"].ToString(); int i = getTotals(userName, userPwd);
if (i > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
} private int getTotals(string strName, string strPwd)
{
int i = 0;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
string s = "select count(*) from login where userName='" + strName + "'and userPwd='" + strPwd + "'";
SqlCommand cmd1 = new SqlCommand(s, con);
con.Open();
i = Convert.ToInt32(cmd1.ExecuteScalar());
con.Close();
return i;
}
顶 3 楼 Open下就好了
希望lz在以后出现错误或者异常的时候可以学会看懂错误提示,这样对自己的调式和以后开发程序的速度有很大的提高
{
string userName = Request.Form["userName"].ToString();
string userPwd = Request.Form["userPwd"].ToString(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'";
SqlCommand cmd1 = new SqlCommand(s,con);
try
{
con.open
int count = Convert.ToInt32(cmd1.ExecuteScalar());
con.close
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.Form["userName"].ToString();
string userPwd = Request.Form["userPwd"].ToString(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'";
SqlCommand cmd1 = new SqlCommand(s,con);
try
{
con.open();//打开连接
int count = Convert.ToInt32(cmd1.ExecuteScalar());
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{//关掉
con.close();
con.dispose();
}
con.open();方法都没有啊,怎么读数据?在读取数据库之前要打开数据库
con.open();
SqlCommand cmd1 = new SqlCommand(s,con);
-------------最后
finally
{
con.close();
}