C#数据库访问未验证

时间:2013-10-30 05:03:40

标签: c# sql database authentication login

请帮助我,我得到了 - MessageBox.Show(“登录不成功,再试一次!”); ----每次我输入正确的用户名并通过。可能是什么问题?谢谢

我似乎想要验证..

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.SqlClient;
    using System.Data.OleDb;

    namespace posSystem
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void btnLogin_Click(object sender, EventArgs e)
            {



                OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=userName.accdb");

                OleDbCommand cmd = new OleDbCommand("Select * From userAndPass Where ID = @id AND pass = @password", conn);
                cmd.Parameters.AddWithValue("@id", txtBoxUserName.Text);
                cmd.Parameters.AddWithValue("@password", txtBoxPassword.Text);
                conn.Open();

                OleDbDataReader re = cmd.ExecuteReader();

                if(re.Read())
                {

                    MessageBox.Show("Login Successfull"); 



                }
                else{
                MessageBox.Show("Login NOT Successfull, Try again!");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

来自MSDN

  

OLE DB .NET提供程序不支持传递的命名参数   SQL语句或由a调用的存储过程的参数   CommandType设置为Text时的OleDbCommand。在这种情况下,   必须使用问号(?)占位符。

尝试更改您的查询:

OleDbCommand cmd = new OleDbCommand("Select * From userAndPass Where ID = ? AND pass = ?", conn);