菜鸟请教C#登陆代码?

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:01:29
protected void btn_Click(object sender, EventArgs e)
{
try
{
string UserID, Password, SqlString;
string ConnString = "Server=(local);User id=sa;Pwd=;;Database=AA"; //AA是数据库名
SqlConnection conn = new SqlConnection(ConnString);//连接字符串
UserID = txtUserID.Text.Trim();
Password = txtPassword.Text.Trim();
SqlString = "select * from User Info where UserID='" + UserID + "' and Password='" + Password + "'";//User Info 是数据表
SqlCommand cmd = new SqlCommand(SqlString, conn); //创建SqlCommand
cmd.CommandType = CommandType.Text;
conn.Open();//打开数据库连接
SqlDataReader dr = cmd.ExecuteReader();
///定义类并获取用户的登陆信息
if (dr.Read())
{
Session["UserID"] = UserID;
Session["Password"] = Password;
Response.Redirect("login.aspx");

}
else
{
txtUserID.Text = "";
txtPassword.Text

不用这个语句
你怎么知道数据库里有这个账号和密码呢,这个语句就是判断数据库里是否有这个账号和密码以及是否一致问题。

至于session 是个会话,记录了此人的账号和密码
在你这段代码 是看不出他们有什么作用,估计在别的功能要用到

Session["UserID"] = UserID;
Session["Password"] = Password
就是说你输入的用户名和密码要和数据库里面的相等~

不加SQL查询语句,那你要数据干什么。
Session保存了用户名和密码

通过查询,如果数据表存在UserID及Password对应的数据,则可以证明用户输入的id和密码正确!!

我没太仔细看,但是从前几句犯了一个硬编码的错误!
它的思路是,先用SA (上面是空密码)登陆数据库!!!,然后去查一张表来对比用户输入的账号密码是否存在。而这条SQL语句,就是用来查找User Info 表中是否有用户名为UserID 密码为Password值的返回。的确,这种连接没有安全性可言。大概是一个作业吧。建议最好使用 Integrated Security

Trusted_Connection
集成验证。