从Access访问C#.net重新获取用户名和密码 [英] Retriving username and password from Access to C#.net

查看:59
本文介绍了从Access访问C#.net重新获取用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要通过访问C#.net来检索用户名和密码我使用以下代码但它无法正常工作,

Hi,
I need to retrive username and password from access to C#.net i''m using the following code but it is not working,

public void login()
      {
 string file_name;
            file_name = Application.StartupPath + "\\" + "username.txt";
            System.IO.StreamWriter objwriter;
            objwriter = new System.IO.StreamWriter(file_name);
            objwriter.WriteLine(textBox1.Text);
            objwriter.Close();
            string nam;
            WeightBridge.Class1 obj = new WeightBridge.Class1();
            nam = obj.get();
            OleDbConnection con = new OleDbConnection(nam);
            //OleDbConnection con = new OleDbConnection("D://weightbridge.mdb");
            con.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT UserName ,pwd FROM User WHERE UserName='" + textBox1.Text + "' AND Password='" + textBox2.Text + "'", con);
            OleDbDataReader dr = cmd.ExecuteReader();
            string userText = textBox1.Text;
            string passText = textBox2.Text;
            while (dr.Read())
            {
                if ((dr["UserName"].ToString() == userText) && (dr["Password"].ToString() == passText))
                {
                    this.Hide();
                    MasterPage mp = new MasterPage();
                    mp.Show();
                }
                else
                {
                    MessageBox.Show("Invalid username");

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

推荐答案

参考:如何:修复错误 - 'microsoft.ace.oledb.12.0''提供程序未在本地计算机上注册 [ ^ ]

Microsoft.ACE.OLEDB.12.0''提供程序未在本地计算机上注册。 [ ^ ]
Refer: HOW TO: FIX ERROR - "the ''microsoft.ace.oledb.12.0'' provider is not registered on the local machine"[^]
Microsoft.ACE.OLEDB.12.0'' provider is not registered on the local machine.[^]


private void button1_Click(object sender ,EventArgs e)

{

if(textBox1.Text ==&& textBox2。文字==)

{

MessageBox.Show(输入用户名和密码);

// label6.Text =需要管理员名称和密码。;

textBox1.Focus();

}

其他

{

string nam;

WeightBridge.Class1 obj = new WeightBridge.Class1();

nam = obj.get();

nam =Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ Application.StartupPath +// weightbridge.accdb; Persist Security Info = False;;

OleDbConnection con = new OleDbConnection(nam);

con.Open();

OleDbCommand cmd = new OleDbCommand(SELECT UserName,pwd FROM [User] WHERE UserName =' '+ textBox1.Text +''AND pwd =''+ textBox2.Text +'',con);

OleDbDataReader dr = cm d.ExecuteReader();

string userText = textBox1.Text;

string passText = textBox2.Text;

if(dr.Read( ))

{



this.Hide();

MasterPage mp = new MasterPage();

mp.Show();





//登录();

dr.Close();

con.Close();

}

else

{

MessageBox.Show(用户名/密码无效);

}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "")
{
MessageBox.Show("Enter the User name and Password");
// label6.Text ="Admin Name and Password Required.";
textBox1.Focus();
}
else
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
nam="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+Application.StartupPath+"//weightbridge.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT UserName ,pwd FROM [User] WHERE UserName=''" + textBox1.Text + "'' AND pwd=''" + textBox2.Text + "''", con);
OleDbDataReader dr = cmd.ExecuteReader();
string userText = textBox1.Text;
string passText = textBox2.Text;
if (dr.Read())
{

this.Hide();
MasterPage mp = new MasterPage();
mp.Show();


//login();
dr.Close();
con.Close();
}
else
{
MessageBox.Show("UserName/Password Invalid");
}


这篇关于从Access访问C#.net重新获取用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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