如何在c#中为Login添加数据库中的数据到变量
本文关键字:数据 变量 数据库 添加 中为 Login | 更新日期: 2023-09-27 18:09:44
我需要做一个登录,并希望创建它与数据库连接。
OleDbCommand cm;
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'LakshanP'Documents'C Sharp work with database'adhideit.accdb";
cm = new OleDbCommand("select usnam,paswd from signin where usnam = @a and paswd = @b", cn);
cm.Parameters.AddWithValue("@usnam", textBox1.Text.ToString());
cm.Parameters.AddWithValue("@paswd", textBox2.Text.ToString());
string un;
string pw;
cn.Open();
OleDbDataReader dr = cm.ExecuteReader();
if (textBox1.Text == "usnam" & textBox2.Text == "paswd")
{
new Form1().Show();
}
else
{
MessageBox.Show("Invalied Username or Password");
}
如果你想从你执行的查询中获得数据,那么你需要从数据读取器(即dr.Read())中读取数据,然后检查它是否有意味着用户名和密码有效的行,否则无效。
OleDbCommand厘米;OleDbConnection cn = new OleDbConnection();cn。ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:'Users'LakshanP'Documents'C Sharp work with database'adhideit.accdb";
cm = new OleDbCommand("select usnam,paswd from signin where usnam = @usnam and paswd = @paswd", cn);
cm.Parameters.AddWithValue("@usnam", textBox1.Text.ToString());
cm.Parameters.AddWithValue("@paswd", textBox2.Text.ToString());
string un;
string pw;
cn.Open();
OleDbDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
new Form1().Show();
}
else
{
MessageBox.Show("Invalied Username or Password");
}