2012-03-02 7 views
0

SQL Serverに対してユーザー名とパスワードを確認できる必要があり、C#フォームアプリケーション用のコードが必要です。C#フォームアプリケーションにSQL認証を追加するにはどうすればよいですか?

2つのテキストボックス(1ユーザーと1パス)でセットアップしてから、ログインボタンがあります。

  SqlConnection UGIcon = new SqlConnection(); 
     UGIcon.ConnectionString = "Data Source=HP-PC//localhost;Initial Catalog=UGI;Integrated Security=True"; 
     UGIcon.Open(); 

     string userText = textBox11.Text; 
     string passText = textBox12.Text; 

     SqlCommand cmd = new SqlCommand("SELECT stUsername,stPassword FROM LoginDetails WHERE stUsername='" + textBox11.Text + "' and stPassword='" + textBox12.Text + "'", UGIcon); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 

     if (dt.Rows.Count > 0) 
     { 
      MessageBox.Show("Login Success!!"); 
      cmd = new SqlCommand("SELECT stRole from LoginDetails where [email protected]", UGIcon); 
      cmd.Parameters.AddWithValue("@stUsername",userText); 
      string role = cmd.ExecuteScalar().ToString(); 
      MessageBox.Show(role); 
      UGIcon.Close(); 
     } 
     else 
     { 
      MessageBox.Show("Access Denied!!"); 
      UGIcon.Close(); 
     } 
+0

にOnclickはのSQLConnectionとSqlCommandオブジェクトを作成し、あなたのDBを照会し、あなたが試してみました何のユーザ – f2lollpll

+0

を読み、確認することができますSqlDataReaderのを取得しますか?だから、 "_I need for code for ... _"と言うのは一般に受け入れられない。私たちはあなたのためにあなたの仕事をするのではなく、あなたを助けるためにここにいます。 –

+0

をopに追加しました。 –

答えて

1

をチェックし、私は "使って" ステートメントを使用しての本当の信者です。元のクエリでstRole変数を尋ねることで、2番目のクエリを自分自身で保存することもできます。使用ブロックは自動的にオブジェクトを破棄します。そのため、実行がこの領域を離れると、オブジェクトは自動的にクリーンアップされます。

using (SqlConnection UGIcon = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=UGI;Integrated Security=True")) 
     { 
      UGIcon.Open(); 

      string userText = textBox11.Text; 
      string passText = textBox12.Text; 

      SqlCommand cmd = new SqlCommand("SELECT stUsername,stPassword, stRole FROM LoginDetails WHERE stUsername='" + userText + "' and stPassword='" + passText + "'", UGIcon); 

      using (SqlDataReader rdr = cmd.ExecuteReader()) 
      { 
       if (rdr.HasRows) 
       { 
        while (rdr.Read()) 
        { 
         string role = rdr["stRole"].ToString(); 
         MessageBox.Show(role); 
        } 
       } 
       else 
       { 
        MessageBox.Show("Access Denied!!"); 
       } 
      } 
     } 
0

Plsはこのコード

SqlConnection thisConnection = new 
     SqlConnection(@"Server=(local)\sqlexpress;Integrated Security=True;" + 
        "Database=northwind"); 
     thisConnection.Open(); 
     SqlCommand thisCommand = thisConnection.CreateCommand(); 
     thisCommand.CommandText = "Select count(*) from UserDetails 
WHere UserName = "+txtUsername.text.trim().toLower() + " and Password = " +txtPassword.text.trim().toLower(); 
     Object countResult = thisCommand.ExecuteScalar(); 
     Console.WriteLine("Count of Customers = {0}", countResult); 

     thisConnection.Close(); 
+0

私はコンソールアプリケーションではないフォームアプリケーションを使用していますが、リモートアドレス用のIPはどこに置き換えますか(ローカル)だけですか? –

+0

@John_Dong - この回答で提供されたコードは、WinFormsに容易に適合させることができます。 –

+0

ええ、私はちょうどあなたがユーザーとパスワードを追加するためにSQLサーバーに送信するためにSQLクエリを持っていることに気付いたのですか? –

関連する問題