我试图根据 C# 和 SQL 中的用户级别显示表单,我得到了带有 User_ID、User_Pass 和 User_Level 的数据表,我希望我的代码查看密码和用户名是否正确并根据用户级别显示表单(1 为经理 2 为员工)非常感谢 :)
private void button1_Click(object sender, EventArgs e)
{
try
{
string connection = @"Data Source=Local-PC\HOME;Initial Catalog=Project;Integrated Security=True";
SqlConnection cn = new SqlConnection(connection);
cn.Open();
string userText = textBox1.Text;
string passText = textBox2.Text;
SqlCommand cmd = new SqlCommand("SELECT ISNULL(User_ID, '') AS User_ID, ISNULL(User_Pass,'') AS User_Pass, User_Level FROM User_Login WHERE User_ID = @User_ID and User_Pass = @User_Pass and User_Level = @User_Level", cn);
cmd.Parameters.Add(new SqlParameter("User_ID", userText));
cmd.Parameters.Add(new SqlParameter("User_pass", passText));
SqlDataReader dr = cmd.ExecuteReader();
try
{
dr.Read();
if (dr["User_ID"].ToString().Trim() == userText && dr["User_pass"].ToString().Trim() == passText && dr["User_Level"].ToString().Trim() == "1")
{
textBox3.Text = dr["User_ID"].ToString();
this.Hide();
Form2 form2 = new Form2();
form2.Show();
//this.Close();
}
if (dr["User_ID"].ToString().Trim() == userText && dr["User_pass"].ToString().Trim() == passText && dr["User_Level"].ToString().Trim() == "2")
{
textBox3.Text = dr["User_ID"].ToString();
this.Hide();
Form3 form3 = new Form3();
form2.Show();
//this.Close();
}
}
catch
{
MessageBox.Show("Invalid Username or Password");
}
dr.Close();
cn.Close();
}
catch
{
}
}