2014-11-11 2 views
-1

나는 데이터베이스에서 날짜로 모든 날짜 값을 가져 왔지만 valye 09/11/2014..its를 수락 할 때 받아 들일 수 있지만 -30/10/2014..its의 값을 부여하면 이런 종류의 오류가 발생합니다. 왜. 도움이 필요합니다 ...varchar 데이터 형식을 datetime 데이터 형식으로 변환하면 범위를 벗어나는 예외가 발생합니다 ..?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    'TODO:add data into data base 
    If ComboBox1.Text <> "" Then 
     Dim ab As Long 
     ab = random.Next("10000", "99999") 


     Dim date1 As Date = Date.ParseExact(TextBox3.Text, "dd/MM/yyyy", Nothing).Date 
     Dim date2 As Date = Date.ParseExact(TextBox4.Text, "dd/MM/yyyy", Nothing).Date 
     Dim date3 As Date = Date.ParseExact(TextBox5.Text, "dd/MM/yyyy", Nothing).Date 
     'Dim date3 As Date = Date.Parse(TextBox5.Text) 
     Dim date4 As Date = Date.ParseExact(TextBox6.Text, "dd/MM/yyyy", Nothing).Date 

     If Button1.Enabled = False Then 
      If TextBox11.Text <> "" And ComboBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" Then 
       TextBox10.Text = ab 
       cm.Close() 
       cm.Open() 
       ComboBox1.Text = Replace(ComboBox1.Text, "'", "''") 
       TextBox1.Text = Replace(TextBox1.Text, "'", "''") 
       TextBox7.Text = Replace(TextBox7.Text, "'", "''") 
       TextBox6.Text = Now.Date.ToString("dd/MM/yyyy") 
       TextBox8.Text = DateTime.Now.ToString("HH:mm:ss") 
       cmd.CommandText = "insert into worker(faccno,fname,faddr,famcdue,fjoindate,fdate,fattended,fproblem,fsolution,fstatus,fremark,fassign,findate,fintime,fserviceno,falert)values ('" & TextBox11.Text & "', '" & ComboBox1.Text & "','" & TextBox2.Text & "','" & date1.ToString("dd/MM/yyyy") & "','" & date2.ToString("dd/MM/yyyy") & "','" & date3.ToString("dd/MM/yyyy") & "','" & ComboBox4.Text & "','" & TextBox1.Text & "','" & TextBox7.Text & "','" & ComboBox2.Text & "','" & ComboBox6.Text & "','" & ComboBox5.Text & "','" & date4 & "','" & TextBox8.Text & "','" & TextBox10.Text & "','" & ComboBox3.SelectedIndex & "')" 
       cmd.ExecuteNonQuery() 
       MessageBox.Show("Information Insertion sucessfull", "Save") 
       MessageBox.Show(ab, "your service no is") 
       Button7.PerformClick() 
       cm.Close() 
      Else 
       MessageBox.Show("enter all values then try to save information", "error") 
       Exit Sub 
      End If 
     ElseIf ComboBox1.Text <> "" Then 
      cm.Close() 
      cm.Open() 
      TextBox1.Text = Replace(TextBox1.Text, "'", "''") 
      TextBox7.Text = Replace(TextBox7.Text, "'", "''") 
      TextBox6.Text = Now.Date.ToString("dd/MM/yyyy") 
      TextBox8.Text = DateTime.Now.ToString("HH:mm:ss") 
      ComboBox4.Text = Replace(ComboBox4.Text, "'", "''") 'TODO:this code replaces single quotes to store in data base 
      cmd.CommandText = "update worker set faccno= '" & TextBox11.Text & "', fname='" & ComboBox1.Text & "',faddr='" & TextBox2.Text & "',famcdue='" & date1 & "',fjoindate='" & date2 & "',fdate='" & date3 & "',fattended='" & ComboBox4.Text & "',fproblem='" & TextBox1.Text & "',fsolution= '" & TextBox7.Text & "',fstatus='" & ComboBox2.Text & "',fremark='" & ComboBox6.Text & "',fassign= '" & ComboBox5.Text & "',findate='" & date4 & "',fintime='" & TextBox8.Text & "',fserviceno='" & TextBox10.Text & "',falert='" & ComboBox3.SelectedIndex & "' where fserviceno='" & TextBox10.Text & "'" 
      cmd.ExecuteNonQuery() 
      MessageBox.Show(" Information Updation sucessfull", "Save") 
      Button7.PerformClick() 
      cm.Close() 
     End If 
    Else 
     MessageBox.Show("enter all values then try to save information", "error") 
     Exit Sub 
    End If 

End Sub 
+0

당신의 코드는 당신이'Replace'를 사용 함에도 불구하고 SQL injection 공격에 취약합니다. 또한 ToString이나 Parse에 대한 명시적인 문화권을 지정하지 않았을 때 누군가 다른 날짜/시간 설정을 사용하면 실패합니다. – Dai

답변

0

여러 가지 가능한 원인이 있습니다. 문자열 연결을 사용하여 SQL 문 ()을 사용하고 있음을 알 수 있습니다. 대신에 - 입력 된 매개 변수를 사용하십시오.) 즉시 두 가지 원인이 나타납니다.

그러나이 원인은 아마도 가장 큰 원인 일 수 있습니다 :

TextBox6.Text = Now.Date.ToString("dd/MM/yyyy") 

... 당신은 dd/MM/yyyy 형식으로 날짜를 변환하고 있지만 슬래시로 구분 된 날짜 형식 MM/dd/yyyy에있는 것으로 예상하여 SQL 서버 - (기본 1033 형식은 미국을 비난)하는 경우 30의 한 달 구성 요소는 의미가 없으므로 실패합니다.

매개 변수를 사용하면 실제 날짜 값 (문자열 표현이 아닌)이 데이터베이스 클라이언트 라이브러리에 전달되므로 매개 변수를 사용하면이 문제가 발생하지 않습니다. 데이터베이스 클라이언트 라이브러리는 다음과 같은 방식으로 올바르게 직렬화 할 수 있습니다. 항상 효과가있을 것입니다.

관련 문제