2014-10-07 107 views
0

我在那里根据用户类型的它会打开一个不同的菜单,但我不知道如何使它识别的类型没有具体说明登录,这是我得到的代码:如何让我的登录识别用户是管理员还是普通用户?

私人无效btnaceptar_Click(对象发件人,EventArgs e) 如果(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(); 
        } 

       } 
      } 
     } 

    } 
    } 

}

对于那些谁不speack西班牙语,usuario意味着用户和contraseña意味着密码,现在我需要实现TIPO这意味着键入

回答

0

在DB名称做一个额外的列它像UserType或其他东西。然后,在选择*之后,只需检查字段值。

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

+0

我得到的领域我只是不知道如何实现它的代码,如果你能告诉我如何将它添加到查询我会apreciate它 – user36379 2014-10-07 19:01:02

+0

@ user36379 IDK你表的定义是什么。但是你可以填充一个DataTable,然后在table.Rows上执行foreach,如果(row [UserType] ==“Admin”)//管理员 – Steve 2014-10-07 19:23:51

1
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 [email protected] AND [email protected] 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 
} 

删除的另一个标志是件好事,在你的代码。这将表示当前用户是否被删除。从数据库中物理删除数据是一种很好的做法。

在这种情况下,您将不会在每次SQL连接时写入数据,而只能查询SqlCommands。我会留下这个来自己计算。

+0

我有一个名为type的列,里面有管理员和员工,但我不知道如何包括没有使用任何对象的列与文本框的用户 – user36379 2014-10-07 19:07:41

+0

@ user36379检查更新我不能更具体。 – mybirthname 2014-10-07 19:20:10

相关问题