如何在使用sql server 2008在c#windows窗体中登录应用程序之前检查用户是否处于活动状态 [英] how to check user is in active state before loggin into the application in c# windows forms with sql server 2008

查看:60
本文介绍了如何在使用sql server 2008在c#windows窗体中登录应用程序之前检查用户是否处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的名字是vishal.i我正在开发和登录表格,在进入申请之前检查用户是否处于活动状态。

以下是我的c#登录代码form( frmLogin ):

hi my name is vishal.i am at process of developing and a login form which checks if user is in active state or not before entering into application.
Given below is my c# code of login form(frmLogin):

using System.Data.SqlClient;
namespace Mini_Project
{
    public partial class frmLogin : Form
    {
public frmLogin()
        {
            InitializeComponent();
        }
 private void btnLogin_Click(object sender, EventArgs e)
        {
             if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
            {
                MDIParent1 h = new MDIParent1();
                h.Show();
                this.Close();
            }
            else
            {
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                bool validUser = ValidateUser(username, password);
                if (validUser)
                {
                    MDIParent1 m = new MDIParent1();
                    m.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Invalid user name or password. Please try with another user name or password", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtUsername.Focus();
                }
            }
        }
        private bool ValidateUser(string username, string password)
        {
            bool success = false;
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select @count = Count(*) from [dbo].[UserDetail] where username=@username and password=@password", conn);
            cmd.Parameters.AddWithValue("@username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.Add("@count", SqlDbType.Int).Direction = ParameterDirection.Output;
            conn.Open();
            cmd.ExecuteNonQuery();
            if (Convert.ToInt32(cmd.Parameters["@count"].Value) > 0)
            {
                success = true;
            }
            else
            {
                success = false;
            }
            conn.Close();
            return success;
        }
    }
}



上面的代码工作正常!

下面给出的是我的结构我的表:sql server 2008中的 UserDetail

ColumnName DataType AllowNulls

user_id(自动增量主键)Int No

user_first_name nvarchar(50)是

user_last_name nvarchar(50)是

user_dob日期是

user_sex nvarchar(20)是

电子邮件nv​​archar(80)是

用户名nvarchar(25)是

密码nvarchar(15)是

user_type Int是

状态位是

状态位是

row_upd_date datetime是

created_by Int Yes



以下是 frmUser c#代码我将新用户添加到应用程序:


The above code works fine!
Given below is my structure of my table:UserDetail in sql server 2008.
ColumnName DataType AllowNulls
user_id(auto-increment primary key) Int No
user_first_name nvarchar(50) Yes
user_last_name nvarchar(50) Yes
user_dob date Yes
user_sex nvarchar(20) Yes
email nvarchar(80) Yes
username nvarchar(25) Yes
password nvarchar(15) Yes
user_type Int Yes
status bit Yes
state bit Yes
row_upd_date datetime Yes
created_by Int Yes

Given below is my c# code of frmUser of how i add new user to application:

namespace Mini_Project
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
        }
 private void btnCreate_Click(object sender, EventArgs e)
        {
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            int autoGenId = -1;
            cmd = new SqlCommand("Insert into [dbo].[UserDetail](user_first_name,user_last_name,user_dob,user_sex,email,username,password,user_type,status,row_upd_date,created_by,state)" + "Values(@user_first_name,@user_last_name,@user_dob,@user_sex,@email,@username,@password,@user_type,@status,GetDate(),@created_by,@state); Select @autoGenId=SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@user_first_name", txtFName.Text);
            cmd.Parameters.AddWithValue("@user_last_name", txtLName.Text);
            cmd.Parameters.AddWithValue("@user_dob", dtDOB.Value);
            if (cboSex.SelectedIndex == 0)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Male");
            }
            else if (cboSex.SelectedIndex == 1)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Female");
            }
            else if (cboSex.SelectedIndex == 2)
            {
                cmd.Parameters.AddWithValue("@user_sex", "Transgender");
            }
            cmd.Parameters.AddWithValue("@email", txtEmailID.Text);
            cmd.Parameters.AddWithValue("@username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@user_type", 0);
            cmd.Parameters.AddWithValue("@status", 1);
            cmd.Parameters.AddWithValue("@Created_by", 1);
            cmd.Parameters.AddWithValue("@state", 1);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 1, txtFName.Text + "User detail was added successfully");
            MessageBox.Show("User Detail was added successfully", "Task", MessageBoxButtons.OK, MessageBoxIcon.Information);
            conn.Close();
            this.Close();
}



所以我有一个数据类型的值(位): 1 这意味着当前用户在活动状态

状态字段值: 0 UserDetail 表中表示当前用户未处于活动状态。

想要的是首先检查该用户是否处于活动状态,然后检查他/她是否以登录形式输入了正确的用户名和密码( frmLogin )。



任何人都可以告诉我在frmLogin(登录表单)的c#代码中我需要做什么修改来检查用户是否处于活动状态然后然后继续验证用户是否输入了正确的用户名和密码?任何人都可以帮助我!任何人都可以帮助我/指导我如何解决我的问题?!任何帮助/指导解决这个问题将不胜感激!


So i have a value of state of data-type(bit):1 which means that current user is in active state
value of state field:0 in UserDetail table indicates current user is not in active state.
What is want is to check first whether that user is in active state or not first and then check if he/she has entered correct user name and password in login form(frmLogin).

Can anyone tell me on what modifications must i need to do in my c# code of frmLogin(login form) to check if user is in active state or not and then go on to validate whether user has entered correct username and password? Can anyone help me please! Can anyone help me/guide me on how to solve my problem?! Any help/guidance in solving of this problem would be greatly appreciated!

推荐答案

尝试在frmLogin中添加需求逻辑如下



Try adding the logic for requirement in frmLogin as follows

using System.Data.SqlClient;
namespace Mini_Project
{
    public partial class frmLogin : Form
    {
public frmLogin()
        {
            InitializeComponent();
        }
 private void btnLogin_Click(object sender, EventArgs e)
        {
             if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
            {
                MDIParent1 h = new MDIParent1();
                h.Show();
                this.Close();
            }
            else
            {
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                if(IsUserActive())
                {
                  bool validUser = ValidateUser(username, password);
                  if (validUser)
                  {
                      MDIParent1 m = new MDIParent1();
                      m.Show();
                      this.Close();
                  }
                  else
                  {
                      MessageBox.Show("Invalid user name or password. Please try with another user name or password", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                      txtUsername.Focus();
                  }
                }
                else
                {
                    MessageBox.Show("User is disabled [Your custom message]", "Task", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.Focus();
                }
            }
        }
        private bool ValidateUser(string username, string password)
        {
            bool success = false;
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select @count = Count(*) from [dbo].[UserDetail] where username=@username and password=@password", conn);
            cmd.Parameters.AddWithValue("@username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.Add("@count", SqlDbType.Int).Direction = ParameterDirection.Output;
            conn.Open();
            cmd.ExecuteNonQuery();
            if (Convert.ToInt32(cmd.Parameters["@count"].Value) > 0)
            {
                success = true;
            }
            else
            {
                success = false;
            }
            conn.Close();
            return success;
        }
//method to check active status
private bool IsUserActive(string username, string password)
        {
            bool success = false;
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select @state = state from [dbo].[UserDetail] where username=@username", conn);
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.Add("@state", SqlDbType.Boolean).Direction = ParameterDirection.Output;
            conn.Open();
            cmd.ExecuteNonQuery();
            success = cmd.Parameters["@state"].Value;
            conn.Close();
            return success;
        }
    }
}





希望,它有帮助:)



Hope, it helps :)


这篇关于如何在使用sql server 2008在c#windows窗体中登录应用程序之前检查用户是否处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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