我有一个user_data表,并且有一个userlevel列我希望访问不同的表单以获取不同的用户级访问权限这里是我的源代码
例如
从user_data中选择用户名,密码,其中username = username,密码= password,userlevel = 1 - 表示form1
从user_data中选择用户名,密码,其中username = username,密码= password,userlevel = 2 - for form2
if ((txtusername.Text.Trim() != "") && (txtpassword.Text.Trim() != "")) // to validate if user and pass have data
{
String passwords = encryption(txtpassword.Text.Trim());
string q = "SELECT username, password FROM user_data WHERE (UserName = '" + txtusername.Text.ToString() + "') AND (Password = '" + passwords + "');";
SqlCommand cmd = new SqlCommand(q, conn);
cmd.Parameters.AddWithValue("@Username", this.txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@Password", this.txtpassword.Text.Trim());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
MessageBox.Show("Successfully Login", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.ShowInTaskbar = false;
this.Hide();
Form1 shForm1 = new Form1();
shForm1.FormClosed += (_, args) => this.Close();
shForm1.ShowDialog();
}
}
if (dr.HasRows == false)
{
MessageBox.Show("Invalid Username and Password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Enter username and password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
任何想法都会让我成为winform开发的初学者 谢谢..
答案 0 :(得分:0)
您所包含的内容"用户名,密码"进入选择?
如果您只需要“用户级访问权限”,则可以使用此权限(或类似内容):
string q = "SELECT **userlevel** FROM user_data WHERE (UserName = '" + txtusername.Text.ToString() + "') AND (Password = '" + passwords + "');";
SqlCommand cmd = new SqlCommand(q, conn);
cmd.Parameters.AddWithValue("@Username", this.txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@Password", this.txtpassword.Text.Trim());
object obj = cmd.ExecuteScalar();
if(obj != null)
{
if((int)obj == 1)
{
Window1 wnd = new Window1();
wnd.Open();
}
else
{
Window2 wnd = new Window2();
wnd.Open();
}
}
else
{
// kick
}
答案 1 :(得分:0)
我认为您需要一家工厂为不同的用户生成不同的表单。 输入用户级别以及工厂的其他参数并获取抽象表格。 像这样:
AbstractForm form = FormFactory.GetForm(userLevel,otherParams...);
您需要先从数据库中读取用户信息。
顺便说一下,FormFactory是这样的:
class FormFacotry
{
//Key is user level and value is Form.
Dictionary<int,Form> formTable=new Dictionary<string,Form>();
static FormFacotry()
{
//Prepare relation between user level and Form.
formTable.Add(1,new Form1());
formTable.Add(2,new Form2());
}
public Form GetForm(int userlevel,...)
{
return formTable[userleval];
}
}