2012-03-02 3 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

는 온 클릭은 SQLConnection을하고있는 SqlCommand을하고 DB를 조회 읽고 당신이 시도 무엇 사용자 – f2lollpll

+0

을 확인할 수있는 SqlDataReader 개체를 얻을? 일반적으로 "_I 코드가 필요합니다 ..."라고 말하는 것은 용납되지 않습니다. 우리는 당신을 돕기 위해 당신을 위해 일하는 것이 아니라 당신을 돕기 위해 왔습니다. –

+0

방금 ​​op에 추가했습니다. –

답변

1

에게 진정한 신자를 확인합니다. 또한 원래 쿼리에서 stRole 변수를 묻는 것으로 두 번째 쿼리를 저장할 수 있습니다. using 블록은 자동으로 객체를 처리하므로 실행이이 영역을 떠날 때 객체는 자동으로 정리됩니다.

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

@ JohnDong -이 답변에서 제공되는 코드는 WinForms에 쉽게 적용될 수 있습니다. –

+0

그래, 방금 사용자와 암호를 추가하기 위해 SQL 쿼리를 SQL 서버에 보내고 싶습니까? –

관련 문제