2016-09-21 7 views
-1

こんにちは私はWindowsフォームアプリケーションを持っており、提供されるログイン資格情報に応じて別のページを開く必要があります。管理者の資格情報(Username = Administrator)が提供されている場合は、「AdminPage」と呼ばれるウィンドウフォームアプリケーションを呼び出す必要があります。そうでなければ "Main_Page"と呼ばれるウィンドウフォームアプリケーションが必要です。ログイン資格情報に依存する特定の「ログイン後のオプション」を設定するにはどうすればよいですか?

私のコードでは、「このコマンドに関連付けられたDataReaderはまだ開いています。まず閉じる必要があります」というメッセージが表示されます。

これは私のコードです。

try 
      { 
       SqlConnection cn = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False"); 
       SqlCommand cmd = new SqlCommand("select * from UserCredentials where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", cn); 
       SqlDataReader dr; 
       cn.Open(); 
       dr = cmd.ExecuteReader(); 
       int cnt = 0; 
       while (dr.Read()) 
       { 
        cnt++; 
       } 
       if (cnt == 1) 
       { 
        MessageBox.Show("Successful Login...", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        string query = "select Username, Password from UserCredentials where Username='Administrator"; 
        SqlCommand cmdA = new SqlCommand(query, cn); 
        dr = cmdA.ExecuteReader(); 
        int k = 0; 
        while(dr.Read()) 
        { 
         k++; 
        } 
        if(k == 1) 
        { 
         AdminPage A_P = new AdminPage(); 
         A_P.Tag = this; 
         A_P.Show(this); 
         Hide(); 
        } 
        Main_Page Mp = new Main_Page(); 
        Mp.Tag = this; 
        Mp.Show(this); 
        Hide(); 
        cn.Close(); 
        textBox1.Clear(); 
        textBox2.Clear(); 
       } 
       else 
       { 
        MessageBox.Show("Invalid UserName or Password", "Message", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning); 
        textBox1.Clear(); 
        textBox2.Clear(); 
       } 
      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 
+3

あなたはSQLインジェクションに対して脆弱です。テキストボックスに '' 'を入れ、何が起こるか見ることができます。パラメータ化されたクエリを代わりに使用します。 –

答えて

0
try { 

SqlConnectionオブジェクトのコン=新しいSqlConnectionオブジェクト( "データソース= PV10 \のLocalServer;初期カタログ= SmallSoftwareDB;統合セキュリティ=がTrue;プール= Falseの"); con.Open();文字列Lg = "Username = '" + textBox1.Text + "' AND password = '" + textBox2.Text + "'"; SqlCommand cmd =新しいSqlCommand(Lg、con); SqlDataReader dr; dr = cmd.ExecuteReader(); int t = 0; while(dr.Read()){t ++; } if(t == 1){MessageBox.Show( "成功したログイン"、 ""、MessageBoxButtons.OK、MessageBoxIcon.Information); ){AdminPage Ap =新しいAdminPage(); Ap.Tag =これ; Ap.Show(これ); textBox1.Clear(); textBox2.Clear();隠す(); } else {Main_Page Mp = new Main_Page();} Mp.Tag = this; Mp.Show(これ); textBox1.Clear(); textBox2.Clear();隠す(); }} else {MessageBox.Show( "無効なユーザー名またはパスワード"、 "、MessageBoxButtons.RetryCancel、MessageBoxIcon.Stop); textBox1.Clear(); textBox2.Clear(); }} catch(Exception ex){MessageBox.Show(ex.Message、 ""、MessageBoxButtons.AbortRetryIgnore、MessageBoxIcon.Warning); }}

関連する問題