简体   繁体   English

如何在从数据库收集数据时通过登录页面在C#中打开不同的表单页面

[英]How can i open different form pages in C# by login page while collecting data from database

I'm making a Grocery store application that connects with a database. 我正在制作一个与数据库连接的Grocery商店应用程序。 I've 3 users at login page Manager Cashier1 and Cashier 2. I want to open form of the relevant user as data is already saved in the database. 我在登录页面管理员Cashier1和Cashier 2上有3个用户。我想打开相关用户的表单,因为数据已经保存在数据库中。 like when I enter Manager and password of manager it should access Manager pager. 比如当我输入管理器的经理和密码时,它应该访问管理器寻呼机。 when i Enter Cashier credentials it should open Cashier page my code is below thank you. 当我输入收银员凭证时,它应该打开收银页面,我的代码在下面谢谢。

I'm using C# windows form application. 我正在使用C#windows表单应用程序。

If it is possible to extract only password field from the database and then compare it with txtUser.text will it work that way or not? 如果可以从数据库中仅提取密码字段,然后将其与txtUser.text进行比较,它是否会以这种方式工作?

    private void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection con1 = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True");
        string query= "select * from ManCash Where USERNAME='"+txtUser.Text+"'and PASS = '"+txtPass.Text+"'";

        SqlDataAdapter sda = new SqlDataAdapter(query, con1);
        DataTable DT = new DataTable();
        sda.Fill(DT);

        if (DT.Rows.Count>0&&DT.Rows.Count<=1)
            {
                this.Hide();
                Manager frm4 = new Manager();
                frm4.ShowDialog();
                this.Close();
            }
        else if(DT.Rows.Count>1&&DT.Rows.Count<=2)
            {
                this.Hide();
                Cashier frm5 = new Cashier();
                frm5.ShowDialog();  
                this.Close();
            }
         else if (DT.Rows.Count==3)
            {
                 this.Hide();   
                 Cashier frm5 = new Cashier();
                 frm5.ShowDialog();
                 this.Close();
            }

You will want to use SQLCOMMAND AND SQLDATAREADER to read the return value of Select statement then put it in a switch statement and make a role for each user. 您将需要使用SQLCOMMAND和SQLDATAREADER来读取Select语句的返回值,然后将其放在switch语句中并为每个用户创建一个角色。 SO that when you login it will automatically match that user on its specific role and open up the exact form. 因此,当您登录时,它将自动匹配该用户的特定角色并打开确切的表单。

Since I don't know how to code in C#, I would like to give you a advice here. 由于我不知道如何用C#编码,我想在这里给你一个建议。

1) Create a new column in your ManCash table and name it 'Role' 1)在ManCash表中创建一个新列,并将其命名为“Role”

2) In your Role column, you may make it as a varchar or int If you made it as varchar put admin if it is for the manager, put staff if not. 2)在你的Role栏,你可以让它为varcharint 。如果你做它作为varcharadmin ,如果它是管理者,把staff如果不。 And if int put 1 for manager and 0 for staff. 如果int为经理提出1 ,为员工提出0

3) Create a query similar to the following: 3)创建类似于以下内容的查询:

(written in pseudo code) (用伪代码编写)

if datatable returns 1 from "Select role from ManCash where username = @username and password = @password" then proceed to next statement

4) Retrieve the value from the roles table, that way, you may segregate the pages to be shown like this pseudo code 4)从角色表中检索值,这样,您可以将要显示的页面隔离为此伪代码

if query returned manager then manager page else staff page

5) I suggest you read the basics of Database management, as a developer, you may want to learn how to manage a database, with this knowledge, you'll learn how to make applications faster, more efficient, and secure. 5)我建议你阅读数据库管理的基础知识,作为开发人员,你可能想学习如何管理数据库,有了这些知识,你将学习如何更快,更高效和安全地使应用程序。

private void btnLogin_Click(object sender, EventArgs e)
{
    SqlConnection con1 = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True");
    string query= "select * from ManCash Where USERNAME='"+txtUser.Text+"'and PASS = '"+txtPass.Text+"'";
SqlCommand cmd=new SqlCommand();
cmd.CommandText="query";
cmd.Connection=con1;
con1.Open();
SqlDataReader reader=cmd.ExecuteReader();
Switch(reader["Role"].toString()){
case "Manager":
//redirect to role form
break;
case "Cashier1":
//redirect to role form
break;
case "Cashier2":
//redirect to role form
break;
default:
break;
}
con1.Close();

if there's an error , try to fix it with your own i'm just giving the Exact logic to get it done :) 如果有错误,请尝试用自己的方法修复它我只是给出了Exact逻辑来完成它:)

It has worked like this without any error. 它没有任何错误就像这样工作。 But thank you all to respond so quickly. 但是,谢谢大家这么快回复。

    SqlConnection con1 = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True");

    private void btnLogin_Click(object sender, EventArgs e)
    {
        string query= "select * from ManCash Where USERNAME='"+txtUser.Text+"'and PASS = '"+txtPass.Text+"'";
        SqlDataAdapter sda = new SqlDataAdapter(query, con1);

        DataTable DT = new DataTable();
        //  SqlCommand command = new SqlCommand("SELECT Role from ManCash Where USERNAME='" + txtUser.Text + "'and PASS = '" + txtPass.Text + "'", con1);
        // SqlDataReader readr = command.ExecuteReader();
        sda.Fill(DT);

        string a = DT.Rows[0]["Role"].ToString();

        switch (a)
        {
            case "Admin":
                this.Hide();
                Manager man = new Manager();
                man.ShowDialog();
                this.Close();
                break;
            case "Staff":
                this.Hide();
                Cashier ca = new Cashier();
                ca.ShowDialog();
                this.Close();
                break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我希望能够使用 id 或用户名验证登录表单,同时使用 c# 从 mysql 数据库中获取数据 - i want to be able to validate a login form with an id or username, while getting the data from mysql database using c# C# 如何从同一个表单中获取数据打开多次,每个表单中都有不同的数据? - C# How to get data from the same form open several times and in each form there are different data? 如何在C#上通过袜子打开https页面? - How I can open https pages via socks on c#? 如何在 C# 和 sql 中调用数据库中的数据? - How can I call the data from the database in C# and sql? C# 如何使用第 1 页中的按钮打开第 2 页? - C# How can i open page 2 with a button in page1? 从C#Winforms中位于Web的服务器数据库中获取数据时,如何使用进度条? - How can i use progress bar while fetching data from server database located in web in c# winforms? 我如何从C#中同一项目下的不同Windows窗体访问不同的组件? - How can i access the different component from different windows form which are under same project in c#? 我如何检查C#Windows应用程序中的窗体是否打开? - How can i check form is open in c# windows application? 如何在 C# 中打开现有表单? - How can I open an existing form in C#? 如何在C#中打开Access数据库? - How can I open an access database in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM