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

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 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机