繁体   English   中英

Windows窗体应用程序数据库:“ System.Data.dll中发生了'System.ArgumentException'类型的未处理异常”

[英]Windows form application database: “An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll”

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

    private void Login_Click(object sender, EventArgs e)
    {

        if ((UserName.Text != "") && (Password.Text != ""))
        {
            string constring = ConfigurationManager.ConnectionStrings["The_patients.Properties.Settings.Users1ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            string q = "select * from Users where UserName = @Username and Password = @Password ";
            SqlCommand cmd = new SqlCommand(q, con);
            cmd.Parameters.AddWithValue("@Username", this.UserName.Text);
            cmd.Parameters.AddWithValue("@Password", this.Password.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("Login Successfully Done");
                }
            }
            if (dr.HasRows == false)
            { MessageBox.Show("Access Denied, password username mismatched"); }
        }
        else { MessageBox.Show("Enter username and password"); }
    }
}
}

我正在尝试将我的应用程序连接到数据库,并检查用户名和密码的有效性,但是当我运行我的代码并编写用户名和密码时,这给了我这个错误,我试图知道出了什么问题,但是我找不到有什么解决办法吗?

该错误在此处显示SqlConnection con = new SqlConnection(constring);

App.config中

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
 <configSections>
 </configSections>
 <connectionStrings>
 <add name="The_patients.Properties.Settings.Users1ConnectionString"
        connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\users\mohammad\documents\visual studio 2015\Projects\The patients\The patients\Users1.mdb"
        providerName="System.Data.OleDb" />
</connectionStrings>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

我在Google中搜索过多,但找不到很好的答案。

您正在使用SqlConnection(以及Sql Server Client库中的所有类),但是您的数据库是MS-Access数据库。 您需要使用System.Data.OleDb命名空间中的OleDbConnection和相对的OleDbXXXX类

OleDbConnection con = new OleDbConnection(constring);

在此过程中,请记住,密码是MS-Access中的保留关键字,因此您需要在方括号中加上方括号

因此,您的代码将被重写

string constring = ConfigurationManager.ConnectionStrings["The_patients.Properties.Settings.Users1ConnectionString"].ConnectionString;
using(OleDbConnection con = new OleDbConnection(constring))
{
    string q = @"select * from Users  
                 where UserName = @Username and [Password] = @Password ";
    using(OleDbCommand cmd = new OleDbCommand(q, con))
    {
        cmd.Parameters.AddWithValue("@Username", this.UserName.Text);
        cmd.Parameters.AddWithValue("@Password", this.Password.Text);
        con.Open();
        using(OleDbDataReader dr = cmd.ExecuteReader())
        {
            if (dr.HasRows == true)
               MessageBox.Show("Login Successfully Done");
            else
               MessageBox.Show("Access Denied, password username mismatched"); 

        }
    }
}

请注意,应将诸如连接,命令和读取器之类的一次性对象封装在using语句中,以确保正确处置这些对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM