由网友(给你一口甜甜)分享简介:我想检查我有连接到我的程序的用户名和密码,我已经做了我的查询,但我将如何去有关检查这对一个表的数据库。私人无效的button1_Click(对象发件人,EventArgs的){菜单M1 =新菜单();字符串的connectionString =供应商= Microsoft.ACE.OLEDB.12.0;数据源= Use...![怎么在SQL数据库中查找程序的登陆用户名和密码](https://p.xsw88.cn/allimgs/daicuo/20230906/2762.png)
我想检查我有连接到我的程序的用户名和密码,我已经做了我的查询,但我将如何去有关检查这对一个表的数据库。
私人无效的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);
}
}
}
![怎么在SQL数据库中查找程序的登陆用户名和密码](https://p.xsw88.cn/allimgs/daicuo/20230906/2762.png)
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);
}
}
}
相关推荐
最新文章