当前位置: 代码迷 >> ASP.NET >> 用户登录界面已做好,想根据数据库a里的user表里的username和password来判断登录是否合法,合法就做下一步,请教这个判断如何写
  详细解决方案

用户登录界面已做好,想根据数据库a里的user表里的username和password来判断登录是否合法,合法就做下一步,请教这个判断如何写

热度:7016   发布时间:2013-02-25 00:00:00.0
用户登录界面已做好,想根据数据库a里的user表里的username和password来判断登录是否合法,合法就做下一步,请问这个判断怎么写?
用户登录界面已做好,想根据数据库a里的user表里的username和password来判断登录是否合法,合法就做下一步,请问这个判断怎么写?


------解决方案--------------------------------------------------------
SqlConnection sqlconn = 。。。。;

SqlCommand sqlcomm = new SqlCommand( "select id from admin where
username= ' "+username+ " ' and userpwd= ' "+userpwd+ " ' ", sqlconn);

sqlconn.Open();

try
{

int uid = (Convert.ToInt64)(sqlcomm.ExecuteScalar());

if (uid > 0)
{
Session[ "username "] = uname;
Session[ "uid "] = uid;
Response.Redirect( "Welcome.aspx ");
}
else
{
Response.Write( " <script language= 'javascript '> alert( '您的用户名或密码错误! '); </script> ");
}

}
catch
{

}
finally
{
sqlconn.Close();
}
------解决方案--------------------------------------------------------
OleDbConnection cnn = new OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=E:\\基于窗体的身份验证\\database.mdb ");
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
OleDbCommand cmd = new OleDbCommand();
string str = "Select Count(*) from people where user_name= ' " + username.Text + " 'and user_password= ' " + Password.Text + " ' ";
cmd.CommandType = CommandType.Text;
cmd.CommandText = str;
cmd.Connection = cnn;
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader.GetValue(0).ToString() != "1 ")
{
msg.Text = "Invalid username and password ";
}
else
{

Response.Redirect( "tt.aspx ");
}
}
  相关解决方案