关于C#.NET用户登录验证的详细代码

来源:百度知道 编辑:UC知道 时间:2024/06/02 16:15:23
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怎么使用和怎样判断用户名和密码数据库中是否存在、对不对。代码要详细,

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("不存在的用户名");
}

}

select count(*) from test where username="'+users+'" and pwd = "'+pwd+'"

然后

cmd.Connection = conn;
int flag =Convert.ToInt32(dr.ExcuteScalar());

if(flag>0)
{
//登陆成