1

我有一个登录名,根据用户的类型,它会打开一个不同的菜单,但我不知道如何让它识别类型而不指定它,这是我得到的代码:

private void btnaceptar_Click(object sender, EventArgs e) { if (txtusuario.Text == "" || txtcontraseña.Text == "") { MessageBox.Show("TODOS LOS CAMPOS DEBEN ESTAR LLENOS.", "ERROR", MessageBoxButtons .OK, MessageBoxIcon.Error); txtusuario.Clear(); txtusuario.Focus(); }

        n = n - 1;
        if (n <= 3 && n >= 0)
        {

            if (n == 1)
            {
                MessageBox.Show("Solo le quedan 1 intento, porfavor asegurese de poner los datos correctos!", "AVISO!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MessageBox.Show("Usuario y/o contraseña incorrectos, verifique porfavor", "Error al ingresar datos.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtusuario.Clear();
                this.txtcontraseña.Clear();
                this.txtusuario.Focus();
            }

            else
            {
                SqlConnection miconexion = new SqlConnection(@"Data Source=USER-PC;Initial Catalog=dbpuntodeventa;Integrated Security=True");
                miconexion.Open();
               SqlCommand comando1 = new SqlCommand("select * from usuarios where usuario='" + txtusuario.Text + "'and contraseña='" + txtcontraseña.Text + "'", miconexion);
                SqlDataReader Ejecuta = comando1.ExecuteReader();

                if (Ejecuta.Read() == true)
                {
                    MessageBox.Show("Bienvenido Administrador , Ingreso de datos correctos", "Ingreso exitoso!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    frmmenuadmin frmprincipal = new frmmenuadmin();
                    frmprincipal.Show();
                    frmprincipal.lblid.Text = txtusuario.Text;
                 }


                else
                {
                    SqlConnection miconexion2 = new SqlConnection(@"Data Source=USER-PC;Initial Catalog=dbpuntodeventa;Integrated Security=True");
                    miconexion2.Open();
                    SqlCommand comando = new SqlCommand("select * from usuarios where usuario='" + txtusuario.Text + "'and contraseña='" + txtcontraseña.Text + "'", miconexion2);
                    SqlDataReader ejecutar1 = comando.ExecuteReader();


                    if (ejecutar1.Read() == true)
                    {

                        MessageBox.Show("Bienvenido Empleado , Ingreso de datos correctos", "Ingreso exitoso!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Hide();
                        frmmenu frm2 = new frmmenu();
                        frm2.Show();
                        frm2.lblnombre.Text = txtusuario.Text;

                    }
                    else
                    {
                        if (n == 0)
                        {
                            MessageBox.Show("Error,se han agotado los intentos", "AVISO!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            Application.Exit();
                        } 
                        MessageBox.Show("Usuario y/o contraseña incorrectos, verifique porfavor", "Error al ingresar datos.", MessageBoxButtons.OK, MessageBoxIcon.Error);                         
                        this.txtusuario.Clear();
                        this.txtcontraseña.Clear();
                        this.txtusuario.Focus();
                    }

                }
            }
        }

    }
    }

}

对于那些不会说西班牙语的人,usuario 表示用户,而 contraseña 表示密码,现在我需要实现 tipo 表示类型

4

2 回答 2

2
public static bool IsAdministrator()
{
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

如果您正在谈论 Windows 用户,请尝试此操作,如果您正在谈论应用程序中的管理员用户,您应该在数据库中有列:IsAdmin或类似的东西。

编辑:

您应该从数据库中获取当前用户和密码的数据,DataTable并检查标志字段 IsAdmin=0 还是 IsAdmin=1。根据结果​​,您将显示正确的菜单。

您还需要使用SqlParameters来防止SqlInjection.

这里简单的代码如何检索数据DataTable

string connectionString = "Your connection";

SqlConnection conn = new SqlConnection(connectionString);

conn.Open();

SqlCommand cmd = new SqlCommand(@"Select * from [User] WHERE UserName=@UserName AND Password=@Password AND Deleted=0", connectionString);

cmd.Parameters.AddWithValue("@UserName", userName.Text);
cmd.Parameters.AddWithValue("@Password", password.Text);

DataSet dst = new DataSet();
string tableName = "Your table Name";

using(SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
    adapter.Fill(dst, tableName);
}

conn.Close();

if(dst.Tables[0].Rows.Count == 0)
//show error

if(dst.Tables[0].Rows.Count > 0)
{
    if(Convert.ToInt32(dst.Tables[0].Rows[0]["IsAdmin"]) == 1)
        //load admin menu
    else
        // load normal user menu
}

Deleted 是另一个标志,可以在您的代码中使用。这将表示当前用户是否被删除。最好不要从数据库中物理删除数据。

在这种情况下,最好在另一个类中进行数据访问,您不会每次都编写 SQL 连接,而只编写 SqlCommands 的查询。我会留下来自己解决。

于 2014-10-07T18:57:06.757 回答
0

在数据库名称中添加一个额外的列,例如 UserType 或其他内容。然后,在您选择 * 之后,只需检查字段值。

PS,如果你想要好的设计模式,那么创建另一个带有 ID 和 TypeName 的 UserType 表,并进行内部连接。但是对于初学者来说是没有必要的。

于 2014-10-07T18:50:14.753 回答