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

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条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法