c#
  • mysql
  • if-statement
  • decimal
  • datareader
  • 2013-06-02 1 views 0 likes 
    0
    MySqlConnection con = new MySqlConnection("host=*;user=*;password=*;database=*;"); 
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM members WHERE username = '" + textBox2.Text + "' AND password = '" + textBox3.Text + "';"); 
    cmd.Connection = con; 
    DataTable dt = new DataTable(); 
    con.Open(); 
    MySqlDataReader reader = cmd.ExecuteReader(); 
    
    if (reader.Read() != false) 
    { 
        if (reader.IsDBNull(0) == true) 
        { 
         cmd.Connection.Close(); 
         reader.Dispose(); 
         cmd.Dispose(); 
         MessageBox.Show("Oops!There was a problem!"); 
        } 
        else 
        { 
         cmd.Connection.Close(); 
         reader.Dispose(); 
         cmd.Dispose(); 
         this.Hide(); 
         Main main = new Main(); 
         main.Show(); 
         MySqlCommand cmmd = new MySqlCommand("SELECT Pain FROM members WHERE username='" + textBox2.Text + "';"); 
         cmmd.Connection = con; 
         con.Open(); 
         MySqlDataReader read = cmmd.ExecuteReader(); 
         if (read.Read()) 
         { 
          if (read.GetDecimal(0) == 1) 
          { 
           MessageBox.Show("NO"); 
           cmmd.Connection.Close(); 
           read.Dispose(); 
           cmmd.Dispose(); 
          } 
          else 
          { 
           MessageBox.Show("YES"); 
           cmmd.Connection.Close(); 
           read.Dispose(); 
           cmmd.Dispose(); 
          } 
        } 
    } 
    else 
    { 
        MessageBox.Show("You Login Information is incorrect!"); 
    } 
    

    나는 PAIN 열을 읽고, 메시지 NO를 표시하고 메시지 YES를 표시하기 위해 1과 같으면 0으로 설정합니다. 통증 Collumn은 INT 유형이고 8 열입니다GetDecimal 작동시키기

    +0

    _ 통증 Collumn은 INT 유형입니다. 그러면 'GetInt32()'를 사용할 수 있습니까? –

    +0

    또한이 오류가 발생합니까? –

    +0

    아니, 지금 좋은, 정말 감사합니다 :) –

    답변

    1

    다음과 같이 시도하십시오.

    using (var con = new MySqlConnection("host=*;user=*;password=*;database=*;")) 
    using (var cmd = con.CreateCommand()) 
    { 
        cmd.CommandText = "SELECT username, Pain FROM members WHERE username = @UserName AND password = @Password"; 
        cmd.Parameters.AddWithValue("@UserName", textBox2.Text); 
        cmd.Parameters.AddWithValue("@Password", textBox3.Text); 
    
        con.Open(); 
        using (var reader = cmd.ExecuteReader()) 
        { 
         if (reader.Read()) 
         { 
          var username = reader.GetString(0); 
    
          if (!reader.IsDBNull(1)) 
          { 
           var pain = reader.GetInt32(1); 
           if (pain == 1) 
           { 
            MessageBox.Show("NO"); 
           } 
           else 
           { 
            MessageBox.Show("YES"); 
           } 
          } 
         } 
         else 
         { 
          MessageBox.Show("You Login Information is incorrect!"); 
         } 
    
        } 
    
    } 
    
    +0

    나는 다음과 같은 오류가있어 : 변환 할 수 없습니다 'int.TryParse (문자열, 밖으로 INT)'에 가장 적합한 오버로드 된 메서드 일치 인수 한 일부 잘못된 인수가 을 'object'to 'string' 상수 값 '1'은 'bool'로 변환 될 수 없습니다. –

    +0

    @VlasiuRobert 업데이트 된 답변 확인 – Damith

    +0

    @VlasiuRobert는 고통 값으로 NULL 값을 허용합니까? – Damith

    관련 문제