求解!用c#写了一个登陆界面,access数据库中有用户名和密码字段,但是在程序里运行的时候,我输入的是对的,但是程序里始终说我的用户名和密码错误,是为什么?真心求解,感激不尽
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace AccessLoginApp
{
public partial class Form1 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form1()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Program Files (x86)\work\Access\EmployeeInfo.accdb;Persist Security Info=False;";
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“employeeDataDataSet.EmployeeData”中。您可以根据需要移动或删除它。
//this.employeeDataTableAdapter.Fill(this.employeeDataDataSet.EmployeeData);
try
{
connection.Open();
checkConnection.Text = "Connection Successful!";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
private void btn_Login_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from EmployeeData where Username=' " + txt_Username.Text + " ' and Password=' " + txt_Password.Text + " ' ";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Username and password is correct");
}
else if (count > 1)
{
MessageBox.Show("Duplicate Username and password");
}
else
{
MessageBox.Show("Username and password is not correct");
}
//if (reader.Read())
//{
// MessageBox.Show("Username and password is correct");
//}
//else
//{
// MessageBox.Show("Username and password is not correct");
//}
connection.Close();
}
}
}