当前位置:编程学习 > C#/ASP.NET >>

我的查询数据库中的用户名怎么老是查不到啊?各位大侠,本人是菜鸟,刚学C#的

ublic class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string username=Request.Form["username"].ToString();
string userpwd=Request.Form["userpwd"].ToString();
SqlConnection con=new SqlConnection("server=.;database=ASP;uid=sa;pwd=;");
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='''+username+''' and pwd='''+userpwd+'''",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfaile.htm");
我的查询数据库中的用户名怎么老是查不到啊?各位大侠,本人是菜鸟,刚学C#的,救命啊 --------------------编程问答-------------------- SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='''+username+''' and pwd='''+userpwd+'''",con); 
应为
SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='“+username+'” and pwd='“+userpwd+”'",con);  --------------------编程问答-------------------- 1:
SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='''+username+''' and pwd='''+userpwd+'''",con); 
应改为:
SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='"+username+"' and pwd='"+userpwd+"'",con); 

2:
string username=Request.Form["username"].ToString(); 
应改为:
if(Request.Form["userpwd"]!=null )
{
string userpwd=Request.Form["userpwd"].ToString(); 
}
3:
string userpwd=Request.Form["userpwd"].ToString(); 
应改为:
if(Request.Form["userpwd"]!=null )
{
string userpwd=Request.Form["userpwd"].ToString(); 
}
--------------------编程问答-------------------- SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='''+username+''' and pwd='''+userpwd+'''",con); 
应为
SqlCommand cmd=new SqlCommand("select count(uid) from userinfo where uid='''+username+''' and pwd='''+userpwd+'''",con); 
int count=Convert.ToInt32(cmd.ExecuteScalar()); 

因为ExecuteScalar()只返回第一行第一列的值,其它的被丢掉. --------------------编程问答-------------------- SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='''+username+''' and pwd='''+userpwd+'''",con); 
int count=Convert.ToInt32(cmd.ExecuteScalar()); 
if (count>0) 

Response.Redirect("main.aspx"); 

else 

Response.Redirect("loginfaile.htm"); 
}
改为:
SqlCommand cmd=new SqlCommand("select count(*) from userinfo where uid='''+username+''' and pwd='"+userpwd+"',con); 
DataSet ds=new DataSet();
DataAdapter da=new DataAdapter(sqlStr,con);
da.Fill(ds);
if(ds.Tables[0].Rows.Count>0)
{
Response.Redirect("main.aspx"); 

else 

Response.Redirect("loginfaile.htm"); 
}

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,