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"); }
}
}
}
私はアプリケーションをデータベースに接続しようとしていますが、私は私のコードを実行し、それは私に、このエラーを与えるのユーザー名とパスワードを作成するとき、私は間違っているかを知ることを試みたが、私はそれがどんな解決策がある見つけることができませんか?Windowsフォームアプリケーションデータベース: "System.Data.dllで 'System.ArgumentException'型の未処理の例外が発生しました"
を示しエラーがApp.configファイル
ここ
SqlConnection con = new SqlConnection(constring);
<?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>
私はグーグルでも多くの検索が、私は良い答えを見つけられませんでした。あなたがSqlConnectionオブジェクト(およびすべてのSQL Serverクライアントライブラリからのクラス)が、あなたのデータベースを使用している
https://msdn.microsoft.com/en-us/library(:)私は、接続文字列が間違っているためにとても残念だったことが判明 –
OKをいただきありがとうございますが、それでも[usingステートメント]を使用してのアドバイスに従ってください/yh598w02.aspx)あなたのアプリケーションを実行し、サーバー応答を維持するために非常に重要です。 – Steve