C#具有两种帐户类型的登录表单

C#具有两种帐户类型的登录表单,c#,login,types,account,C#,Login,Types,Account,我必须选择不同类型的帐户:管理员和学生。我希望管理员能够打开RecordStudent表单和其他表单,而Student类型只能打开Subject表单 登入表格: namespace WindowsTest { public partial class LogIn : Form { SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=dbEnroll;Int

我必须选择不同类型的帐户:管理员和学生。我希望管理员能够打开RecordStudent表单和其他表单,而Student类型只能打开Subject表单

登入表格:

namespace WindowsTest
{
    public partial class LogIn : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=dbEnroll;Integrated Security=True");
        SqlCommand cmd;
        SqlDataReader dr;
        public LogIn()
        {
            InitializeComponent();
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            con.Open();
            string sql = "select*from tblAccount where Type='Administrator' or Type='Student' and Username like '%" + txtUsername.Text + "%' and Password like '%" + txtPassword.Text + "%'";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            try
            {
                if (dr.HasRows == true)
                {
                    txtUsername.Text = dr[1].ToString();
                    txtPassword.Text = dr[2].ToString();
                    MessageBox.Show("Successfully Login", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    Enrollment enroll = new Enrollment(); 
                    enroll.Show();
                    this.Hide();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid Username or Password", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            dr.Close();
            con.Close();
        }

欢迎来到StackOverflow!看起来您发布了登录表单的代码,但不是决定用户是否可以打开特定表单的代码。请您显示代码并准确描述哪些不起作用?在编辑代码时,请确保使用SQL参数化查询而不是字符串连接,以避免“SQL注入”注释。只是不要这样做。我可以用你的应用程序在一瞬间擦除你的数据库。。。您当前的代码存在许多问题。桌面代码很容易被反编译,所以您的安全措施相当于隐蔽的安全措施,不提供真正的防御。您需要向体系结构(web服务器)添加一个中间层,并让应用程序从web服务器获取数据。现在,您可以使用ASP.NET中现有的成员资格/身份验证框架,并使对数据库的访问远离客户端…您似乎还将密码存储为纯文本。。。所以现在我也可以抓取你所有的用户名和密码。不要尝试使用自己的身份验证。要做到正确是非常困难的。你可能有其他人还没有解决的问题需要集中精力解决。