protected void btn_ok_Click(object sender, EventArgs e)
{
string conn;
string users, pwd;
users = username.Text;
pwd = userpwd.Text;
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=;pwd=;");
SqlConnection con = SqlConnection(Sql, conn);
con.Open();
SqlCommand dr = new SqlCommand("select count(*) as icount from test where username="+users+"");
}
这是我写的一部分代码,现在就是我不那个count怎么使用和怎样判断用户名和密码数据库中是否存在、对不对。代码要详细,
关于C#.NET用户登录验证的详细代码
答案:5 悬赏:0 手机版
解决时间 2021-12-26 03:24
- 提问者网友:疯孩纸
- 2021-12-25 18:07
最佳答案
- 五星知识达人网友:长青诗
- 2021-12-25 19:14
protected void btn_ok_Click(object sender, EventArgs e)
{
string conn;
string users, pwd;
users = username.Text;
pwd = userpwd.Text;
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=;pwd=;");
SqlConnection con = SqlConnection(Sql, conn);
con.Open();
SqlCommand dr = new SqlCommand("select count(*) as icount from test where username="+users+"");
SqlDataAdapter sda=new SqlDataAdapter(dr);
DataTable dt=new DataTable();
sda.Fill(dt);
con.Close();
if(dt.Rows.Count>=1)
{
Response.Redirect("跳转页面");
}
else
{
Response.Write("不存在的用户名");
}
}
{
string conn;
string users, pwd;
users = username.Text;
pwd = userpwd.Text;
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=;pwd=;");
SqlConnection con = SqlConnection(Sql, conn);
con.Open();
SqlCommand dr = new SqlCommand("select count(*) as icount from test where username="+users+"");
SqlDataAdapter sda=new SqlDataAdapter(dr);
DataTable dt=new DataTable();
sda.Fill(dt);
con.Close();
if(dt.Rows.Count>=1)
{
Response.Redirect("跳转页面");
}
else
{
Response.Write("不存在的用户名");
}
}
全部回答
- 1楼网友:春色三分
- 2021-12-25 23:41
string conn,users,pwd;
users=username.text;
pwd = userpwd.Text;
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=;pwd=;");
SqlConnection con = SqlConnection(Sql, conn);
con.Open();
SqlCommand dr = new SqlCommand("select * from test where username="+users+"");
SqlCommand dr1 = new SqlCommand("select * from test where username="+users+""and password="+pwd+" );
Data.SqlClient.SqlDataReader myreader = dr.ExecuteReader;
Data.SqlClient.SqlDataReader myreader1 = dr1.ExecuteReader;
if myreader.read() then
if myreader1.read() then
'登陆
else
'密码错误
endif
else
'没有该用户
endif
- 2楼网友:千杯敬自由
- 2021-12-25 22:13
select username,password from test where username="+users+" and password="+pwd+"
然后执行并查看结果行数,要是一行都没有就是密码不正确了
- 3楼网友:第幾種人
- 2021-12-25 21:16
是这样的,count(*)是数据返回的行数,如果你输入的用户名和密码存在,则count返回的数字应该为1,如果不存在则为0(数据库中不存在该用户),所以if(count(*)!=0){则数据库存在该用户}else{用户不存在}
string sql=string.format("select count(*) from test where username={0}and pwd={1}", username.Text,userpwd.Text);注:username为数据库表中用户的用户名,pwd为密码都是表中的字段名
- 4楼网友:低血压的长颈鹿
- 2021-12-25 20:19
select count(*) from test where username="'+users+'" and pwd = "'+pwd+'"
然后
cmd.Connection = conn;
int flag =Convert.ToInt32(dr.ExcuteScalar());
if(flag>0)
{
//登陆成功逻辑
}
else
{
//登陆失败逻辑
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯