2016-10-05 4 views
0

これは私が登録情報に使用しているコードです.2つのoleDbCommandを一緒に追加することで様々な方法を試しましたが、これはうまく動作しません。TryCatchコードをすべて検索する方法はありますかtbname.Textのユーザー名を重複していますか?テーブル内のユーザ名が重複しています

Select count(*) from [User] Where [email protected] 

まず@UserName、これはそれがに追加されたパラメータである:だからそれは文句を言わない、この名前を持つユーザーをカウントするクエリを作成し、データベースに挿入すると、エラーメッセージ

 { 

      { 
       connect.Open(); 
       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connect; 
       command.CommandText = "select * from Table1 where Username ='" + tbName.Text + "'"; 

       OleDbDataReader reader = command.ExecuteReader(); 
       int count = 0; 
       while (reader.Read()) 
       { 
        count = count + 1; 
        //count++; 
       } 
       if (count == 0) 
       { 
        if (tbName.Text != "Name" && tbPass.Text != "Password") 
        { 
         if (tbEmail.Text != "Email" && tbMobile.Text != "Number") 
         { 
          if (tbFirstName.Text != "" && tbLastName.Text != "") 
          { 
           const int MIN_LENGTH = 8; 

           string password = tbPass.Text; 

           if (password.Length >= MIN_LENGTH && upperCase(password) >= 1) 
           { 
            r2.Text = ""; 
            r2.ForeColor = Color.Red; 
           } 
           else 
           { 
            r2.Text = "*Password Is Bad*"; 
            r2.ForeColor = Color.Red; 
           } 
           if (RegularExpression.checkForEmail(tbEmail.Text.ToString())) 
           { 
            r3.Text = ""; 
           } 
           else 
           { 
            r3.Text = "Invalid email ! Email Contains a @ , .Com "; 
            r3.ForeColor = Color.Red; 
           } 
           if (r2.Text == "" && r3.Text == "") 
           { goto na; } 
           else { goto ne; } 
          } 
          else { goto ne; } 
         } 
         else { goto ne; } 
        } 
        else { goto ne; ; } 
       ne: 
        if (tbName.Text == "Username") 
        { r1.Text = "*USERNAME REQUIRED*"; r1.ForeColor = Color.Red; } 
        if (tbPass.Text == "Password") 
        { r2.Text = "*PASSWORD REQUIRED*"; r2.ForeColor = Color.Red; } 
        if (tbEmail.Text == "Email") 
        { r3.Text = "*EMAIL REQUIRED*"; r3.ForeColor = Color.Red; } 
        if (tbMobile.Text == "Number") 
        { r4.Text = "*MOBILE NUMBER REQUIRED*"; r4.ForeColor = Color.Red; } 
        if (tbFirstName.Text == "") 
        { label3.Text = "*FIRST NAME REQUIRED*"; r4.ForeColor = Color.Red; } 
        else { label3.Text = ""; } 
        if (tbLastName.Text == "") 
        { label4.Text = "*LAST NAME REQUIRED*"; r4.ForeColor = Color.Red; } 
        else { label4.Text = ""; } 
        MessageBox.Show("Please fill up all the required information correctly before proceeding"); 
        return; 
       na: 

        try 
        { 
         connect.Open(); 
         OleDbCommand command1 = new OleDbCommand(); 
         command1.Connection = connect; 
         command1.CommandText = "insert into Table1([Username], [Password], [Email], [Number], [FirstName], [LastName]) values('" + tbName.Text + "','" + tbPass.Text + "','" + tbEmail.Text + "','" + tbMobile.Text + "','" + tbFirstName.Text + "','" + tbLastName.Text + "')"; 


         command1.ExecuteNonQuery(); 
         MessageBox.Show("Data Saved"); 

         MessageBox.Show("Successfully registered, Please log in"); 
         regPage log = new regPage(); 
         this.Hide(); 
         log.ShowDialog(); 
         this.Close(); 
         connect.Close(); 
        } 
        catch (Exception ex) 
        { 
         // MessageBox.Show("Error " + ex); 
         connect.Close(); 
        } 


       } 
       // if (count > 1) 
       // { 
       //  MessageBox.Show("Duplicate username and password"); 
       // } 
       else 
       { 
        // MessageBox.Show("Username and password is incorrect"); 
        MessageBox.Show("Duplicate Name , Please Use Other Username"); 

        regPage log = new regPage(); 
        this.Hide(); 
        log.ShowDialog(); 
        this.Close(); 
        connect.Close(); 
       } 


       connect.Close(); 



      } 
     } 
+0

Gah! SQLの注入穴。それは私たちを燃やす! –

+0

ちょうど古い学校の方法を使用して@JoelCoehoorn –

+0

@テロンキー私が何を書いているのか、何かが明らかでないかどうか質問を理解しようとする。 – mybirthname

答えて

3

テイクユーザー入力を表示しますcommand.Parameters.AddWithValue("@UserName", tbName.Text)のコマンド。これはあなたのコードをSql Inejctionから守る必要があります。

countクエリの結果が得られた場合count = 0 =>エラーメッセージが表示され、メソッドが終了した場合、count == 0の場合は挿入クエリを続行します。

は注意してください:

のOleDbCommandとOdbCommandは、名前付きパラメータをサポートして使用していないのですか?代わりにプレースホルダを使用するため、パラメータの順序が重要です。ただし、可読性のために、?を使用する代わりに、そのパラメータに名前を付けることができます。

したがって、正しく動作させるには、2番目のコマンドのパラメータが正しい順序になっている必要があります。

 connect.Open(); 
    OleDbCommand command = new OleDbCommand(); 
    command.Connection = connect; 

    command.CommandText = "insert into Table1([Username], [Password], [Email], [Number], [FirstName], [LastName]) values(@Param1,@Param2,'" + tbEmail.Text + "','" + tbMobile.Text + "','" + tbFirstName.Text + "','" + tbLastName.Text + "')"; 
    command.ExecuteNonQuery(); 

    command.Paramaters.AddWithValue(@Param1, yourValue); 
    command.Paramaters.AddWithValue(@Param2, yourValue); 
    and so on for other your parameters but with correct order 

@ Param1以外の名前を使用することができます。また、表の名前をTable1という名前にしないでください。

もう1つのことは、tryとcatchで接続を閉じる必要はないということです。正しいアプローチあなたがトライキャッチのtry/catchを使用している使用する場合/ついに

try 
{ 
    con.Open(); 
    // stuff 
} 
catch 
{ 
    throw; 
} 
finally 
{ 
    //this will be executed always 
    con.Close(); 
} 

行う別の事前の事は別のクラスでのデータアクセスを持つことです。私はこれが学校プロジェクトだと思いますが、あなたが本当のプログラマーになりたいと思って何かを学びたいのであれば、データアクセスクラスがどのようにこの質問に書かれているか確認できます:checking user name or user email already exists。答えは、SQL接続についてですが、それは同じです。

可読性を高めることができるもう1つの点は、tbでテキストボックスを開始しないことです。名前の最後にコントロールを示します。あなたの目は正しい名前をすぐにキャッチしないので。したがって、多くのコントロールがある場合、userNameTxt、userNameTbはtbUserNameよりも優れています。

+0

私のコードをしばらく変更して、別の方法を試してみると、何が間違っているのを見ることができますか?それはカウントに入るつもりのないユーザー名を示しています。== 1 @mybirthname –

+0

@TeronKeeあなたの質問にコメントを書きました。何がデバッグされているかを学び、何が起こっているかを正しく見るために使います。 – mybirthname

+0

私はデータベースに保存するための登録情報のようにやっているので、同時に両方の接続を使用することはできません。そして、私はこの@mybirthnameでまだ新しいので、あなたのフォーマットを理解していません –

関連する問題