2017-01-14 14 views
1
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クライアントライブラリからのクラス)が、あなたのデータベースを使用している

答えて

2

は、MS-アクセスです。あなたはそれでいる間、そのパスワードは、MS-Accessで予約済みのキーワードであることを覚えているので、あなたはその周りに角括弧を必要とするのSystem.Data.OleDb名前空間

OleDbConnection con = new OleDbConnection(constring); 

からOleDbConnectionオブジェクトとの相対OleDbXXXXクラスを使用する必要があります

だから、あなたのコードの書き換えは、接続、コマンド、読者のような使い捨てのオブジェクトは、これらのオブジェクトの正しい処分を確保するために、usingステートメントで囲む必要がありますことを

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"); 

     } 
    } 
} 

お知らせとなります。

+0

https://msdn.microsoft.com/en-us/library(:)私は、接続文字列が間違っているためにとても残念だったことが判明 –

+0

OKをいただきありがとうございますが、それでも[usingステートメント]を使用してのアドバイスに従ってください/yh598w02.aspx)あなたのアプリケーションを実行し、サーバー応答を維持するために非常に重要です。 – Steve

関連する問題