need4spdzz 2021-03-17 17:56 采纳率: 33.3%
浏览 206
已采纳

C#连接access数据库,用户名和密码始终是错误的是为什么?

求解!用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();
            }
     }
}

  • 写回答

3条回答 默认 最新

  • 多鱼的夏天 2021-03-17 18:42
    关注

    可以把这个sql 打印出来在access里运行一下,看看结果

    command.CommandText = "select * from EmployeeData where Username=' " + txt_Username.Text + " ' and Password=' " + txt_Password.Text + " ' ";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标