asp.net (c#) 连接数据库 验证用户登陆
3个回答
展开全部
private void btnLogin_Click(object sender, EventArgs e)
{
string constr = "Data Source=SQL2000;Initial Catalog=login;Integrated Security=True";//你要与自己的数据库建立联系,我这边用的是windows验证
mysqlconnection = new SqlConnection(constr);
mysqlconnection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM user WHERE
name='" + this.txtName.Text + "' and pass='" + this.txtPasswd.Text +
"'",mysqlconnection);
string t = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (t!="0")
{
Server.Transfer("right.aspx?Feedback="...."");//跳到right.aspx;
}
else
{
Server.Transfer("wrong.aspx?Feedback="..."");//跳到wrong.aspx;
}
}
{
string constr = "Data Source=SQL2000;Initial Catalog=login;Integrated Security=True";//你要与自己的数据库建立联系,我这边用的是windows验证
mysqlconnection = new SqlConnection(constr);
mysqlconnection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM user WHERE
name='" + this.txtName.Text + "' and pass='" + this.txtPasswd.Text +
"'",mysqlconnection);
string t = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (t!="0")
{
Server.Transfer("right.aspx?Feedback="...."");//跳到right.aspx;
}
else
{
Server.Transfer("wrong.aspx?Feedback="..."");//跳到wrong.aspx;
}
}
展开全部
一般是这样做的:
页面上输入用户名(user)和密码(pass),根据输入去(logoin)中检索:select count(*) from logoin where admin=user and pwd=pass
判断count(*)>0,正确,否则不正确
,还有什么不明白的?
页面上输入用户名(user)和密码(pass),根据输入去(logoin)中检索:select count(*) from logoin where admin=user and pwd=pass
判断count(*)>0,正确,否则不正确
,还有什么不明白的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
SqlConnection sconn = new SqlConnection();
SqlCommand scmd = new SqlCommand();
DataSet ds = new DataSet();
protected void Login_Click(object sender, EventArgs e)
{
string UserName = user_TextBox.Text.Trim();
string Password = password_TextBox.Text.Trim();
scmd.CommandText = "select * from logoin where admin='" + UserName + "' and pwd='" + Password + "'";
sconn.ConnectionString = connstr;
scmd.Connection = sconn;
sconn.Open();
SqlDataReader dr = scmd.ExecuteReader();
if (dr.Read())
{
Session["UserName"] = dr["admin"].ToString().Trim();
Session["Password"] = dr["pwd"].ToString().Trim();
Response.Redirect("Index1.aspx");//登陆成功跳转页面
}
else
Label1.Text = "用户名和密码无效,请重试!!!!";
}
我感觉你应该都能看懂吧,你如果学过asp.net
SqlConnection sconn = new SqlConnection();
SqlCommand scmd = new SqlCommand();
DataSet ds = new DataSet();
protected void Login_Click(object sender, EventArgs e)
{
string UserName = user_TextBox.Text.Trim();
string Password = password_TextBox.Text.Trim();
scmd.CommandText = "select * from logoin where admin='" + UserName + "' and pwd='" + Password + "'";
sconn.ConnectionString = connstr;
scmd.Connection = sconn;
sconn.Open();
SqlDataReader dr = scmd.ExecuteReader();
if (dr.Read())
{
Session["UserName"] = dr["admin"].ToString().Trim();
Session["Password"] = dr["pwd"].ToString().Trim();
Response.Redirect("Index1.aspx");//登陆成功跳转页面
}
else
Label1.Text = "用户名和密码无效,请重试!!!!";
}
我感觉你应该都能看懂吧,你如果学过asp.net
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |