如何在C#中打开特定帐户的特定表单 [英] How to open a specific form for a specific account in C#

查看:63
本文介绍了如何在C#中打开特定帐户的特定表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在使用C#和PostgreSQL(作为数据库)为大学开发应用程序。



那一刻,我在登录表格和应该出现的下一个表格上工作。

登录表格包含用户名,密码和连接按钮。

数据库表用户包含用户名,密码并输入。



我有两种帐户类型:老师和学生。还有两个主要表格:教师使用的主要表格1和学生使用的主要表格2.



当用户使用分配给教师的帐户登录时,登录表单将关闭,教师表单将打开。和学生一起。

(账户类型:老师 - >主表格1)

(账户类型:学生 - >主表格2)



到目前为止,表单已完成,但我仍在尝试弄清楚如何编写代码,以便数据库检查帐户类型,然后打开所需的表单。



我问是否有人可以请求帮助或分享建议。



下面我发布了Connect按钮的代码在登录表单中:

 private void btnConnect_Click(object sender,EventArgs e)
{
try
{
string myConnection = Server = x; Port = x; Database = x; UID = x; Pwd = x;

NpgsqlConnection myConn = new NpgsqlConnection(myConnection);
NpgsqlCommand myComm = new NpgsqlCommand(select * from Users where user ='+ this.tb_user.Text +'and password ='+ this.tb_pw.Text +';,myConn);

NpgsqlDataReader myReader;
myConn.Open();
myReader = myComm.ExecuteReader();
int count = 0;
while(myReader.Read())
{
count = count + 1;
}
if(count == 1)
{
MessageBox.Show(用户和密码正确... \ nConnected!,大学);

this.Hide();
MainForm1 f1 = new MainForm1(Welcome,+ tb_user.Text.ToUpper());
f1.ShowDialog();
}
else
{
MessageBox.Show(用户和密码不正确!,大学);
}

myConn.Close();
}
catch(异常消息)
{
MessageBox.Show(msg.Message);
throw;
}
}



提前谢谢。

解决方案

你需要开发一个逻辑来确定登录的用户类型,在数据库中创建一个新列,以保存用户是教师还是学生。它可以是单个位列,名称为IsTeacher,如果用户是教师,则可以保存1,如果用户是学生,则可以保存0。



一次完成查询并登录用户。您可以获取该用户ID或用户名的详细信息。



  //  如果登录的用户是教师 
如果(User.IsTeacher){
// 创建表单1实例,并显示它
Form1 form = new Form1();
// 否则,
} else {
// 创建表单2实例并显示
Form2 form = new Form2();
}





..这是您可以使用的基本逻辑。要根据用户显示表单,请记住逻辑始终基于条件。没有条件,你永远不能创建逻辑。一旦你得到了这个条件,就可以开发逻辑。


myreader [type]将包含数据库为用户返回的值。根据这种类型,您可以在显示主窗体周围放置一个开关或if-else,它可以是MainForm1或MainForm2。



但是,如果你期望的话一个结果,您可以更改:

  while (myReader.Read())



into:

  if (myReader.Read() )



使代码更容易。

  private   void  btnConnect_Click( object  sender,EventArgs e)
{
< span class =code-keyword> try
{
string myConnection = Server = x; Port = x; Database = x; UID = x; Pwd = x;

NpgsqlConnection myConn = new NpgsqlConnection(myConnection);
NpgsqlCommand myComm = new NpgsqlCommand( select来自用户=' + .tb_user.Text + 'and password =' + this .tb_pw.Text + ';,myConn);

NpgsqlDataReader myReader;
myConn.Open();
myReader = myComm.ExecuteReader();

if (myReader.Read())
{
MessageBox.Show( 用户和密码正确... \ nConnected! 大学);

this .Hide();

switch (myReader [ 输入]。ToString())
{
case teacher
MainForm1 f1 = new MainForm1( Welcome, + tb_user.Text.ToUpper());
f1.ShowDialog();
break ;
case student
MainForm2 f2 = new MainForm2( 欢迎, + tb_user.Text.ToUpper());
f2.ShowDialog();
break ;
默认
MessageBox.Show( 错误...);
break ;
}
}
else
{
MessageBox.Show( 用户和密码不正确! 大学);
}

myConn.Close();
}
catch (异常消息)
{
MessageBox.Show(msg.Message);
throw ;
}
}



注意处理读取器和连接等对象的方式和时间。


如上所述,如果角色不会改变,if语句也能正常工作。有时我喜欢使用字典作为'switch'语句。类似于:





  var  lut =  new 字典< string,>>(StringComparer.InvariantCultureIgnoreCase); 

lut.Add( teacher,str = > new MainForm1( 欢迎, + str));
lut.Add( student,str = > new MainForm2( 欢迎, + str));
// 此处可以为其他角色添加更多表单
lut。添加( superadmin,str = > new SuperForm(str));
lut.Add( techguy,str = > new TechForm(str));


// ...跳过一些行
if (myReader.Read()){
string type = myReader [ type]。ToString();
if (lut.ContainsKey(type)){
lut [type](tb_user.Text.ToUpper())。ShowDialog();
}
}

#IF DEBUG
foreach var kp in lut){
kp.Value( DEBUG)显示(); // 无理由显示
}
#ENDIF


Hello,
I am developing an application for a University, using C# and PostgreSQL(as the database).

For the moment, I work on Login Form and the next Form that should appear.
The Login Form contains Username, Password and Connect button.
The database table Users contains username, password and type.

I have two account types: teacher and student. And also two Main Forms: the Main Form1 that teachers use and the Main Form2 that students use.

When the user log in with an account that is assigned to teacher, the login form will close and the teacher form will open. And so with the student.
(account type: teacher -> Main Form1)
(account type: student -> Main Form2)

So far, the forms are done, but I am still trying to figure out how to write the code so that the database will check the account type and then open the desired form.

I am asking if anyone can please help or share an advice.

Below I posted the code for Connect button in Login form:

private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
            string myConnection = "Server=x; Port=x; Database=x; UID=x; Pwd=x";

            NpgsqlConnection myConn = new NpgsqlConnection(myConnection);
            NpgsqlCommand myComm = new NpgsqlCommand("select * from Users where user='" + this.tb_user.Text + "' and password= '" + this.tb_pw.Text + "'; ", myConn);

            NpgsqlDataReader myReader;
                myConn.Open();
                myReader = myComm.ExecuteReader();
                int count = 0;
                while (myReader.Read())
                {
                    count = count + 1;
                }
                if (count == 1)
                {
                    MessageBox.Show("User and Password are correct...\nConnected!", "University");

                    this.Hide();
                    MainForm1 f1 = new MainForm1("Welcome, " + tb_user.Text.ToUpper());
                    f1.ShowDialog();
                }
                else
                {
                    MessageBox.Show("User and Password are incorrect!", "University");
                }

                myConn.Close();
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg.Message);
                throw;
            }
        }   


Thank you in advance.

解决方案

You need to develop a logic to determine what type of user has logged in, inside your Database create a new column, to save whether the user is a teacher or a student. It can be a single Bit column, with name of IsTeacher, and you can save 1 if the user is a teacher or 0 if the user is a student.

Once done query-ing and loggin the user in. You can get the details for that user ID or user name that user has.

// If the user that is logged in, is teacher
if(User.IsTeacher) {
   // create the form 1 instance, and show it
   Form1 form = new Form1();
   // otherwise, 
} else {
   // create the form 2 instance and show it
   Form2 form = new Form2();
}



..this is a basic logic that you can use. To show the form depending on the user, remember the logic is always based on a condition. Without a condition you can never create a logic. Once you've got the condition, you can develop the logic.


myreader["type"] would contain the value return from the database for the user. Based on that type, you can put either a switch or if-else around showing the Main form, which can either be MainForm1 or MainForm2.

However, if you expect a single result, you could change:

while (myReader.Read())


into:

if (myReader.Read())


to make the code easier.

private void btnConnect_Click(object sender, EventArgs e)
{
    try
    {
    string myConnection = "Server=x; Port=x; Database=x; UID=x; Pwd=x";

    NpgsqlConnection myConn = new NpgsqlConnection(myConnection);
    NpgsqlCommand myComm = new NpgsqlCommand("select * from Users where user='" + this.tb_user.Text + "' and password= '" + this.tb_pw.Text + "'; ", myConn);

    NpgsqlDataReader myReader;
        myConn.Open();
        myReader = myComm.ExecuteReader();

        if (myReader.Read())
        {
            MessageBox.Show("User and Password are correct...\nConnected!", "University");

            this.Hide();

            switch (myReader["type"].ToString())
            {
                case "teacher":
                    MainForm1 f1 = new MainForm1("Welcome, " + tb_user.Text.ToUpper());
                    f1.ShowDialog();
                    break;
                case "student":
                    MainForm2 f2 = new MainForm2("Welcome, " + tb_user.Text.ToUpper());
                    f2.ShowDialog();
                    break;
                default:
                    MessageBox.Show("Error...");
                    break;
            }
        }
        else
        {
            MessageBox.Show("User and Password are incorrect!", "University");
        }

        myConn.Close();
    }
    catch (Exception msg)
    {
        MessageBox.Show(msg.Message);
        throw;
    }
}   


Pay attention to how and when you dispose of objects like the reader and the connection.


As mentioned above, if statement works well if the roles won't change. Sometimes I like to use a dictionary as a 'switch' statement. Something like:


var lut = new Dictionary<string,>>(StringComparer.InvariantCultureIgnoreCase);

lut.Add("teacher", str => new MainForm1("Welcome, "+str));
lut.Add("student", str => new MainForm2("Welcome, "+str));
// can add more forms here for other roles, too
lut.Add("superadmin", str=> new SuperForm(str));
lut.Add("techguy", str => new TechForm(str));


//... skipping some lines
if (myReader.Read()){
    string type = myReader["type"].ToString();
    if(lut.ContainsKey(type)){
        lut[type](tb_user.Text.ToUpper()).ShowDialog();
    }
}

#IF DEBUG
    foreach(var kp in lut){
        kp.Value("DEBUG").Show(); // show all for no reason
    }
#ENDIF


这篇关于如何在C#中打开特定帐户的特定表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆