我该如何验证登录 [英] How do I go about validating login

查看:89
本文介绍了我该如何验证登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我现在所做的,我有两个文本框和两个按钮,prett标准名称我的approch是这样的。



Here is what I have done up til now, I have two text boxes and two buttons, with prett standard names my approch is like this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccessLogin
{
    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();
        }
        OleDbConnection connect = new OleDbConnection();

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
            string UserName = txtUserName.Text;
            string PassWord = txtPassWord.Text;
            connect.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT UserName, PassWord FROM TAdminPW WHERE"); 
        }

    }
}





现在我想我正在建立联系我需要逻辑,我应该看到输入的用户名和密码存在于DB中,如果是这样我可以访问主程序。

提前感谢你的帮助



So now I think I am makeing connection and I need the logic where I should see that the entered user name and pass word exists in the DB if so the I can have access to the main program.
Thanks in advance for your assistence

推荐答案

试试这个:

Try this:
string connectionString= @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
string sql= "select count(*) from TAdminPW where UserName=@UserName and [PassWord]=@Password"
using(OleDbConnection con = new OleDbConnection(connectionString))
using(OleDbCommand cmd = new OleDbCommand(sql, con))
{
     con.Open();
     cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
     cmd.Parameters.AddWithValue("@Password", txtPassWord.Text);
     int result = (int)cmd.ExecuteScalar()
     if(result > 0)
          MessageBox.Show("Login Successful.");
     else
         MessageBox.Show("Login Failed!");
}


Hai

解决方案1工作正常,如果你想要你的编码方式试试这个



Hai
Solution 1 is work fine,if u want ur coding way try this

   private void btnOk_Click(object sender, EventArgs e)
        {
            
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
DataTable dt1 = new DataTable();
string strcmd1 = "";
OleDbDataAdapter cmd1 = new OleDbDataAdapter;
            string UserName = txtUserName.Text;
            string PassWord = txtPassWord.Text;
            connect.Open();
           strcmd1  = ("SELECT UserName, PassWord FROM TAdminPW WHERE UserName='" & txtUserName.Text.Trim() & "' and Password='" & txtPassWord.Text.Trim() & "'");


cmd1 = new OleDbDataAdapter(new OleDbCommand(strcmd1));
cmd1.SelectCommand.Connection = connect;

cmd1.Fill(dt1)
if ((dt1.Rows.Count > 0)) {
	//redirect to main page
} else {
	//show worng username or password error msg
}
        }
 
    }
}







问候

Aravind




Regards
Aravind


试试类似的事情,

Try something like,
private void btnOk_Click(object sender, EventArgs e)
{
    connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
    string UserName = txtUserName.Text;
    string PassWord = txtPassWord.Text;
    connect.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM TAdminPW WHERE UserName='"+userName.Txt+"',  Password='" +passwordTxt.Text+ "'"); 
    int n = cmd.ExecuteNonQuery();
    
    if(n > 0)
        MessageBox.Show("Login Successful.");
    else
        MessageBox.Show("Login Failure.");
}



-KR


-KR


这篇关于我该如何验证登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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