检查数据库的用户名或密码OledDb连接 [英] Check Database for username or password OledDb Connection
本文介绍了检查数据库的用户名或密码OledDb连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查我有连接到我的程序的用户名和密码,我已经做了我的查询,但我将如何去有关检查这对一个表的数据库。
私人无效的button1_Click(对象发件人,EventArgs的)
{
菜单M1 =新菜单();
字符串的connectionString =供应商= Microsoft.ACE.OLEDB.12.0;数据源= UserAccounts.accdb;坚持安全信息= FALSE;;
OleDbCommand的CMD =新的OleDbCommand(SELECT * FROM UserAccounts其中用户名=+ userBox.Text +和密码=+ Password.Text +);
使用(OleDbConnection的康恩=新的OleDbConnection(的connectionString))
{
尝试
{
conn.Open();
this.Hide();
m1.Show();
}
赶上(例外EXC)
{
的MessageBox.show(exc.Message);
}
}
}
解决方案
您是pretty的已近,让我们只修改一点点:
私人无效的button1_Click(对象发件人,EventArgs的)
{
菜单M1 =新菜单();
字符串的connectionString =供应商= Microsoft.ACE.OLEDB.12.0;数据源= UserAccounts.accdb;坚持安全信息= FALSE;;
使用(OleDbConnection的康恩=新的OleDbConnection(的connectionString))
{
尝试
{
conn.Open();
使用(OleDbCommand的CMD =新的OleDbCommand(选择UserAccounts *其中用户名= @用户名和密码= @Password))
{
cmd.Parameters.AddWithValue(@用户名,userBox.Text);
cmd.Parameters.AddWithValue(@密码,Password.Text);
使用(OleDbDataReader R = cmd.ExecuteReader())
{
如果(r.HasRows)
{
//做的东西在这里
}
}
}
this.Hide();
m1.Show();
}
赶上(例外EXC)
{
的MessageBox.show(exc.Message);
}
}
}
I'm trying to check a database that i have connect to my program for user name and password i have my query made but how would i go about checking it against a table.
private void button1_Click(object sender, EventArgs e)
{
Menu m1 = new Menu();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=UserAccounts.accdb; Persist Security Info=False;";
OleDbCommand cmd = new OleDbCommand("Select * from UserAccounts where Username = " +userBox.Text + " and Password] = " + Password.Text + "");
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
try
{
conn.Open();
this.Hide();
m1.Show();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
解决方案
You're pretty close already, let's just modify it a little bit:
private void button1_Click(object sender, EventArgs e)
{
Menu m1 = new Menu();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=UserAccounts.accdb; Persist Security Info=False;";
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
try
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("Select * from UserAccounts where Username = @Username and Password = @Password"))
{
cmd.Parameters.AddWithValue("@Username", userBox.Text);
cmd.Parameters.AddWithValue("@Password", Password.Text);
using (OleDbDataReader r = cmd.ExecuteReader())
{
if (r.HasRows)
{
// do something here
}
}
}
this.Hide();
m1.Show();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
这篇关于检查数据库的用户名或密码OledDb连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文