本文介绍了如何在C#中打开特定帐户的特定表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 我正在使用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; } } 提前谢谢。解决方案 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. 解决方案 这篇关于如何在C#中打开特定帐户的特定表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-01 20:59