2017-04-25 29 views
0

私はUsername、Passwordという名前のテキスト列とgroupという整数列を持つLoginTableというアクセステーブルを持っています。 Username_txtBxと呼ばれるテキストボックスとDepartment_cmbBxというコンボボックスを持つAddUserというWindowsフォーム。 Add_btnというボタンもあります。ボタンのクリックイベントで次のコードを持つユーザーを追加できます。 しかし、どうすればいいですか?データベースを検索して、ユーザー名がすでに存在するかどうかを確認し、メッセージボックスをスローしてユーザーに通知し、以下のコードを実行しない場合はチェックします。私はSQLデータベースの多くの例を見つけましたが、Accessデータベースの例はありません。名前のアクセスデータベースを確認してください

try 
{ 
    int g = new int(); 

    if (Department_cmbBx.SelectedItem.ToString() == "Office") 
    { 
     g = 1; 
    } 
    else if (Department_cmbBx.SelectedItem.ToString() == "Stores") 
    { 
     g = 2; 
    } 
    else if (Department_cmbBx.SelectedItem.ToString() == "Workshop") 
    { 
     g = 3; 
    } 
    else if (Department_cmbBx.SelectedItem.ToString() == "Management") 
    { 
     g = 4; 
    } 
    else if (Department_cmbBx.SelectedItem.ToString() == "Admin") 
    { 
     g = 5; 
    } 

    connection.Open(); 
    OleDbCommand command = new OleDbCommand(); 
    command.Connection = connection; 
    command.CommandText = "insert into LoginTable(Username,[Password],[Group]) values ('" + Username_txtBx.Text + "','password'," + g + ")"; 
    command.ExecuteNonQuery(); 
    connection.Close(); 
    Username_txtBx.Text = ""; 
    Department_cmbBx.Text = "";     
} 
catch (Exception ex) 
{ 
    MessageBox.Show("error " + ex); 
} 
+0

SELECTクエリが必要です。 – john

+0

SQLまたはselectクエリへのアクセスは変更されません。SQL – Krishna

+0

の例を使用してください。データベースにパスワードを平文で格納しているようです。これは重大なセキュリティ上の脆弱性です。あなたは決してこれを行うべきではありません。パスワードはハッシュ化され、塩分化されたものでなければなりません。パスワードのハッシュフォームを比較して、それらを検証する必要があります。 – mason

答えて

2

まず、あなたのif文は、より効率的で読みやすいswitchの文に置き換えることができます。

第2に、次のselectクエリでOleDbDataReaderを使用して、ユーザー名が既にテーブルに存在するかどうかを確認できます。

ご注意SQLコマンドを文字列として書く場合、より信頼性が高く、ベストプラクティスのCommand.Parameters.Addを使用しています。

int g; 
bool UserExists = false; 

switch(Department_cmbBx.SelectedItem.ToString()) 
{ 
    case "Office": 
     g = 1; 
     break; 
    case "Stores": 
     g = 2; 
     break; 
    case "Workshop": 
     g = 3; 
     break; 
    case "Management": 
     g = 4; 
     break; 
    case "Admin": 
     g = 5; 
     break; 
    default: 
     MessageBox.Show("error: an invalid value."); 
     break; 
} 

using (OleDbConnection connection = new OleDbConnection(connectionString)) 
{ 
    using (OleDbCommand command = new OleDbCommand("select [Username] from LoginTable where [email protected]" , connection)) 
    { 
     command.Parameters.Add("@Username", Username_txtBx.Text); 
     connection.Open(); 

     using(OleDbDataReader reader = command.ExecuteReader()) 
     { 
      // If at least 1 row was returned, this means the user exists in the table. 
      while (reader.Read()) 
      { 
       UserExists = true; 
      } 
     }  

     if (!UserExists) 
     { 
      // The user does not exists - you can create it. 
      command.Parameters.Clear(); 
      command.CommandText = "insert into LoginTable([Username],[Password],[Group]) values (@Username,@Username,@G)"; 
      command.Parameters.Add("@Username", Username_txtBx.Text); 
      command.Parameters.Add("@Password", "password"); 
      command.Parameters.Add("@G", g); 
      command.ExecuteNonQuery(); 
     } 
     else 
     { 
      // Show an error message - the user already exists 
      MessageBox.Show("The user you eneterd already exists."); 
     } 
    } 
} 
+0

SQLコマンドの文字列連結を推奨しないでください。 – Nino

+0

答えは正しいですが、私はSQLインジェクションを避けるためにパラメータの使用を提案します。ここでは例を見ることができますhttps://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx –

+0

@Ninoあなたは100%正しいですが、私はコマンドパラメタについてOPを教えようとしていないので、彼は学習過程にあるように思えるので、あまりにも多くの変更を加えることなく質問に答えることにしました。 –

0

おかげメイソン私はいくつかの理由で動作するようにコードを取得するが、それからswitchステートメントを使用していたし、他の人のような選択クエリは言っていませんでした。私は次のコードを使用して終了しました

   bool UserExists = false; 
      command.CommandText = "Select [Username] from LoginTable where Username = '" + Username_txtBx.Text + "'"; 
      OleDbDataReader reader = command.ExecuteReader(); 

      int g = new int(); 
      while (reader.Read()) 
      { 
       UserExists = true; 
      } 
      connection.Close(); 
      if (!UserExists) 
      { 
関連する問題