使用c#windows应用程序在登录详细信息中设置角色 [英] set roles in login details using c# windows application

查看:100
本文介绍了使用c#windows应用程序在登录详细信息中设置角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i在我的代码中几乎没有帮助。



i由c#开发与Windows应用程序



我的流程是:,



i设置了三个角色,而登录详细信息



他们是,



1.user



2.admin



3.others



这些都包含我的三个角色,我实际完成了这个过程但是我的代码意味着什么,



如果我输入正确的用户名和密码意味着它的工作正常但是,



如果我假设输入错误的用户名和密码意味着我一次得到以下三次错误消息,







else {MessageBox.Show(请检查您的用户名/密码或检查您的角色,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Exclamation); }








所以请检查下面的完整代码并找出我的位置在我的代码中犯了错误。



请在此处提出解决方案..



代码是:

hi all,

i have few assistance in my code below.

i have developed by c# with windows application

my flow is :,

i have set three roles while login details

they are,

1.user

2.admin

3.others

these are all contains my three roles, i actually done this process but what happened my code means,

if i enter correct user name and password means its working fine but,

if i suppossed to type wrong user name and password means i got the given below error message thrice at a time ,



else { MessageBox.Show("Please check your username/Password or else check your Role", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }




so please check my full code below and find out where i would made mistake in my code.

please drop a solution here..

code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace tarriff_design_page
{
    public partial class sample : Form
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        SqlDataReader dr;

        string s = @"Data Source=.\SQLEXPRESS;AttachDbFilename=Database1.mdf;Integrated Security=True;User Instance=True";
      
        public sample()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {



            if (checkBox1.Checked)
            {
                try
                {
                   
                    con = new SqlConnection(s);
                    con.Open();
                    cmd = new SqlCommand("select username,password from login", con);
                    cmd.ExecuteNonQuery();
                    dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        string strname = textBox1.Text;
                        string strpwd = textBox2.Text;

                        if (dr["username"].ToString() == strname && dr["password"].ToString() == strpwd)
                        {
                            Home hpage = new Home();
                            hpage.Text = ("Welcome to ") + textBox1.Text + ("  ") + System.DateTime.Now.ToString();
                            hpage.Show();

                            this.Hide();

                        }

                        else
                        {
                            MessageBox.Show("Please check your username/Password or else check your Role", this.Text,
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        }


                        dr.Close();
                        con.Close();
                    }

                }

                catch (Exception ex1)
                {

                }

            }
           

                if (checkBox3.Checked)
                {
                    try
                    {

                        con = new SqlConnection(s);
                        con.Open();
                        cmd = new SqlCommand("select username,password from role3", con);
                        cmd.ExecuteNonQuery();
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            string strname = textBox1.Text;
                            string strpwd = textBox2.Text;

                            if (dr["username"].ToString() == strname && dr["password"].ToString() == strpwd)
                            {

                                searchgv vd = new searchgv();
                                vd.Text = ("Welcome to ") + textBox1.Text + ("  ") + System.DateTime.Now.ToString();

                                //vd.btnprv.Hide();
                                vd.Show();

                                this.Hide();
                            }

                            else
                            {
                                MessageBox.Show("Please check your username/Password or else check your Role", this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            }

                            dr.Close();
                            con.Close();
                        }

                    }

                    catch (Exception ex1)
                    {

                    }

                }

                if (checkBox2.Checked)
                {
                    try
                    {
                        
                        con = new SqlConnection(s);
                        con.Open();
                        cmd = new SqlCommand("select username,password from adminlogin", con);
                        cmd.ExecuteNonQuery();
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            string strname = textBox1.Text;
                            string strpwd = textBox2.Text;

                            if (dr["username"].ToString() == strname && dr["password"].ToString() == strpwd)
                            {
                                Admin ad = new Admin();
                                ad.Text = ("Welcome to ") + textBox2.Text + ("  ") + System.DateTime.Now.ToString();
                                ad.Show();
                                this.Hide();
                            }

                            else
                            {
                                MessageBox.Show("Please check your username/Password or else check your Role", this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            }
                        }
                        con.Close();
                        dr.Close();

                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message.ToString());
                    }

                }
               

            }

      
            
    }
}





提前感谢...



thanks in advance...

推荐答案

因为你检查了3次..

使用goto语句.. .exampal

because ur checking it 3 times..
use goto statement... exampal
if(i==2)
  goto endThis;

if(g==6)
  i=g;
endThis:



你的消息后使用goto并在关闭方法之前设置endpart

也适用于google上的goto搜索goto在c #windows应用程序


after ur message use goto and set endpart before closing of method
also for goto search on google for "goto in c# windows application"


我想这会对你有帮助....



i think this will help you....

if (cboRole.Text == "NORMAL USER")
                {
                    try
                    {
                        objCon.Open();
                        SqlCommand objCom = new SqlCommand("SELECT * FROM LOGIN WHERE USERNAME='" + this.txtUserName.Text.Trim() + "' and PASSWORD='" + this.txtPassword.Text.Trim() + "' AND USER_ROLE='" + this.cboRole.Text.Trim() + "'", objCon);
                        SqlDataReader dr = objCom.ExecuteReader();
                        if (dr.HasRows)
                        {
                            MessageBox.Show("Login Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Form1 objForm = new Form1();
                            objForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("User Name or password is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtUserName.Text = "";
                            txtPassword.Text = "";
                            txtUserName.Focus();
                        }
                    }
                    catch (SqlException er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        objCon.Close();
                    }
                }
                else if (cboRole.Text == "SUPER USER")
                {
                    try
                    {
                        objCon.Open();
                        SqlCommand objCom = new SqlCommand("SELECT * FROM LOGIN WHERE USERNAME='" + this.txtUserName.Text.Trim() + "' and PASSWORD='" + this.txtPassword.Text.Trim() + "' AND USER_ROLE='" + this.cboRole.Text.Trim() + "'", objCon);
                        SqlDataReader dr = objCom.ExecuteReader();
                        if (dr.HasRows)
                        {
                            MessageBox.Show("Login Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Form1 objForm = new Form1();
                            objForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("User Name or password is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtUserName.Text = "";
                            txtPassword.Text = "";
                            txtUserName.Focus();
                        }
                    }
                    catch (SqlException er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        objCon.Close();
                    }
                }
                
                else if (cboRole.Text == "ADMIN")
                {
                    try
                    {
                        objCon.Open();
                        SqlCommand objCom = new SqlCommand("SELECT * FROM LOGIN WHERE USERNAME='" + this.txtUserName.Text.Trim() + "' AND PASSWORD='" + this.txtPassword.Text.Trim() + "' AND USER_ROLE='" + this.cboRole.Text.Trim() + "'", objCon);
                        SqlDataReader dr = objCom.ExecuteReader();
                        if (dr.HasRows)
                        {
                            MessageBox.Show("Login Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.RetUserName = (string)txtUserName.Text;
                            Form1 objForm = new Form1();
                            objForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("User Name or password is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtUserName.Text = "";
                            txtPassword.Text = "";
                            txtUserName.Focus();
                        }
                    }
                    catch (SqlException er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        objCon.Close();
                    }
                }


这是因为满足所有3条件条件。



您可以检查if else条件或使用角色切换案例构造。



此外,代码除了作为sql查询的参数传递的角色更改之外,块似乎是相同的。因此可以考虑将冗余代码移动到以角色作为参数的方法。
It happens since all the 3 if conditions are satisfied.

You can check the if else condition or switch case construct with the roles .

Also the code blocks appears to be the same except the role passed as the parameter for the sql query only change. So can be thought of moving the redundant code to a method which takes roles as parameter.


这篇关于使用c#windows应用程序在登录详细信息中设置角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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