登陆代码(1)

SqlConnection conn = new SqlConnection("uid=sa;pwd=*;database=login;server=server"); 
   SqlCommand myCom = conn.CreateCommand(); 
   myCom.CommandType = CommandType.Text; 

   if(txtB_user.Text.Length == 0) 
   { 
    MessageBox.Show("用户不能为空,请查询后输入"); 
 
    return; 
   } 

   myCom.CommandText = "SELECT COUNT(*) FROM login WHERE username='"+txtB_user.Text+"'"; 

   conn.Open(); 

   if((int)myCom.ExecuteScalar() == 0) 
   { 
    MessageBox.Show("不存在此用户,请检查后重新输入","错误信息",MessageBoxButtons.OK,MessageBoxIcon.Error ); 

    return; 
   } 
   myCom.CommandText = "SELECT username,pass FROM login WHERE username='"+txtB_user.Text+"'"; 

   SqlDataReader myReader = myCom.ExecuteReader(); 

   if(myReader.Read()) 
   { 
    if(txtB_pass.Text == myReader[1].ToString() && txtB_user.Text.Length != 0) 
    { 
     MessageBox.Show("登录成功","恭喜",MessageBoxButtons.OK,MessageBoxIcon.Information); 
                    Form2 frm2 =new Form2();
     frm2.Show();
  
    } 
    else 
    { 
     MessageBox.Show("用户" + txtB_user.Text + "的密码不正确","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning); 
     txtB_pass.Focus(); 
     return; 
    } 
   }  
本博客有部分内容来自网络,如有问题请联系:hebeilijianghua@qq.com,并注明来自博客园。
原文地址:https://www.cnblogs.com/leebokeyuan/p/5586680.html