Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OLEDBEException Unhandled:没有为一个或多个参数提供值_C# - Fatal编程技术网

C# OLEDBEException Unhandled:没有为一个或多个参数提供值

C# OLEDBEException Unhandled:没有为一个或多个参数提供值,c#,C#,以前尝试只使用用户名和密码创建登录页面。 但是,由于我的用户表有3个角色,因此我想创建一个登录页面,根据用户的角色向其授予登录权限 例如:管理员对管理员页面、员工对员工页面等 尝试执行此操作时,在我的一行中遇到以下错误: OLEDBEException未处理,没有为一个或多个参数提供值 这是我的登录码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u

以前尝试只使用用户名和密码创建登录页面。 但是,由于我的用户表有3个角色,因此我想创建一个登录页面,根据用户的角色向其授予登录权限

例如:管理员对管理员页面、员工对员工页面等

尝试执行此操作时,在我的一行中遇到以下错误: OLEDBEException未处理,没有为一个或多个参数提供值

这是我的登录码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace AcuSapp
{
    public partial class Login : Form
    {
        OleDbConnection LoginLink = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\Visual Studio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb");
        public Login()
        {
            InitializeComponent();
            //textBox_username.Text = "LittleJohn";
            //textBox_password.Text = "HelloJohn";
        }


        private void button_login_Click(object sender, EventArgs e)
        {
             string username = textBox_username.Text;
            string password = textBox_password.Text;
            string role_name = comboBox_role.Text;

            //this is to give notification if username and password is lesser than 4 characters
            // .length will count the characters in the string
            // This is to reduce redundant calls. Less calls = less taxing on the db
            if ((username.Length < 4) || (password.Length < 4))
            {
                MessageBox.Show("Wrong Credentials!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                // Set authentication as false. By default, user is not authenticated yet.
                bool isAuthenticated = false;
                //Opens the connection to db
                LoginLink.Open();
                // Sets the SQL command to be executed
                // Since it is a variable command, it becomes a new SQL command to be executed in Microsoft access
                // + is to join the string together
                //Does string comparing to see if username and password match exactly, case sensitive.

                //var cmd = new OleDbCommand("SELECT COUNT(*) FROM [User] WHERE username = '" + username + "' AND password = '" + password + "' ", LoginLink);
                var cmd = new OleDbCommand("SELECT COUNT(*) FROM [User] WHERE STRCOMP(username, '" + username + "', 0) = 0 AND STRCOMP(password, '" + password + "', 0) = 0 AND STRCOMP(role_name, '" + role_name + "', 0) = 0", LoginLink);
                // (int)cmd.ExecuteScalar only reads the first few rows from the db
                isAuthenticated = (int)cmd.ExecuteScalar() == 1; //Error on this line.
                //Closes connection to db
                LoginLink.Close();
                // if isAuthenticated is true
                if (isAuthenticated)
                {
                    // This will open the next page which is form1
                    Client hello = new Client(this);
                    hello.Show();
                    // Hides the login form
                    this.Hide();
                }
                else
                {
                    //Always remember to put the last statement in curly braces
                    //otherwise it wont show the previous error will show this messsage instead
                    MessageBox.Show("Wrong Credentials!");
                }
            }


        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Data.OleDb;
名称空间ACUSAP
{
公共部分类登录:表单
{
OleDbConnection LoginLink=new-OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\visualstudio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb”);
公共登录()
{
初始化组件();
//textBox_username.Text=“LittleJohn”;
//textBox_password.Text=“HelloJohn”;
}
私有无效按钮\u登录\u单击(对象发送者,事件参数e)
{
字符串username=textBox\u username.Text;
字符串密码=textBox\u password.Text;
字符串role\u name=comboBox\u role.Text;
//这是为了在用户名和密码少于4个字符时发出通知
//.length将计算字符串中的字符数
//这是为了减少冗余呼叫。更少的呼叫=减少对数据库的负担
if((username.Length<4)| |(password.Length<4))
{
显示(“错误的凭证!”,“错误”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
其他的
{
//将身份验证设置为false。默认情况下,用户尚未进行身份验证。
bool isAuthenticated=false;
//打开与数据库的连接
LoginLink.Open();
//设置要执行的SQL命令
//由于它是一个变量命令,因此它将成为一个新的SQL命令,在Microsoft access中执行
//+是将字符串连接在一起
//比较字符串以查看用户名和密码是否完全匹配,区分大小写。
//var cmd=new OleDbCommand(“从[User]中选择COUNT(*),其中username='“+username+”,password='“+password+”,LoginLink);
var cmd=new OleDbCommand(“从[User]中选择COUNT(*),其中STRCOMP(username,“+username+”,0)=0,STRCOMP(password,“+password+”,0)=0,STRCOMP(role_name,“+role_name+”,0)=0,LoginLink);
//(int)cmd.ExecuteScalar仅读取数据库中的前几行
isAuthenticated=(int)cmd.ExecuteScalar()==1;//此行出错。
//关闭与数据库的连接
LoginLink.Close();
//如果isAuthenticated为true
如果(未经验证)
{
//这将打开下一页,即form1
客户hello=新客户机(此);
你好,Show();
//隐藏登录表单
this.Hide();
}
其他的
{
//永远记得把最后一句话用大括号括起来
//否则它不会显示以前的错误,而是显示此消息
MessageBox.Show(“错误的凭据!”);
}
}
}
}
}

您的连接字符串看起来有点不正确。您定义了两个数据源,其中一个数据源被分配了一个不是有效数据源的提供程序:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\Visual Studio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb"
您应该删除以下部分:

Data Source=Provider=Microsoft.ACE.OLEDB.12.0;
尝试下面的连接字符串可能会有所帮助

 OleDbConnection LoginLink = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\Visual Studio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb");

你这里有很多问题。首先,您很容易受到SQL注入攻击。其次,如果您的用户有多个角色,会发生什么情况?(发生的事情比你想象的要多。)制作一个存储过程来验证你的用户名/密码,或者至少参数化你的输入。然后,用一个链接表把你的角色和你的用户分开。啊,我想我明白我的问题了。和STRCOMP(角色名称“+”角色名称“+”,0)=0”,LoginLink);//(int)cmd.ExecuteScalar仅读取数据库中的前几行“既然我使用的是组合框,我该怎么办?Well ExecuteScalar从数据库中读取第一列第一行,如Microsoft在此处所述。。。如果您只是返回1或0,这是正常的。对于组合框问题,您可能需要执行以下操作:string role\u name=comboBox\u role.SelectedValue这将为您提供用户选择的项目。无法编辑我的上一条评论,因此我正在添加另一条评论。comboBox\u role.SelectedValue将为您提供用户选择的“值”。如果需要所选文本,可以使用comboBox\u role.SelectedText