用户登录界面已做好,想根据数据库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 ");
}
}