2017-01-24 14 views
0

그래서 내가 VB.Net 내부 응용 프로그램 벌금을 실행할 수는 folling 코드는 다음과 같습니다방지 SQL 주입 및 사용 SqlDataAdapter를

Private Sub LoadAttachments() 
     tablegrid = New DataTable 
     myConn = New SqlConnection("Server=CEDASDSOBSQL02\dev; Database=Insurance; Integrated Security=true") 
     myConn.Open() 
     myCmd = myConn.CreateCommand 
     Dim query As String = "SELECT DocType, docyear, CASE WHEN docmonth IS NULL THEN NULL " & _ 
        "WHEN docmonth = '0' THEN '- All Months -' WHEN docmonth >= 1 AND docmonth <= 12 " & _ 
        "THEN DATENAME(month, DATEADD(month, docmonth, -1)) END DocMonth, DID from dbo.Document where XALASKAID = '" & LicenseNumber & "' and DOCTYPE like '%Report%'" 
     da = New SqlDataAdapter(query, myConn) 
     myCmd = New SqlCommand(query, myConn) 
     myCmd.CommandType = CommandType.Text 
     da = New SqlDataAdapter(myCmd) 
     da.Fill(tablegrid) 
     DataGridView3.DataSource = tablegrid 
     Label4.Text = "Found " & DataGridView3.Rows.Count & " images" 
    End Sub 

그래서이 코드는 SQL 주입 XALASKAID = '" & LicenseNumber & "'이며, 그 쿼리 안에 있습니다. '" & LicenseNumber & "'을 사용하는 대신 다음과 같이 변경해야합니다. 다음을 입력하십시오. myCmd.Parameters.Add("@LicID", SqlDbType.Int) myCmd.Parameters("@LicID").Value = LicenseNumber

매개 변수를 사용하여 LicenseNumber에 들어갈 때 모두 null 또는 비어 있습니다. 또한 나는 내 코드에서 내가 코딩을 너무 많이 재사용하고 있다고 생각하는 조수가 필요하다. 약간 단순화하는 것이 가능하다면, 고마워요.

추신 : 전 세계적으로 선언했습니다.

+0

당신은 * 어떻게 * LicenseNumber에 대한 매개 변수를 사용하고 있는지 보여줄 수도 있습니다. – Plutonix

+0

우리는 작동하지만 (SQL 주입 가능) 코드를 보여줬고, 우리에게 보여주지 않은 다른 코드가 왜 isn인지 일하는거야? – David

+0

위의 코드는 정상적으로 작동하지만 문제는 쿼리 내에서' ' "& LicenseNumber &"' '를 사용할 수 없기 때문에 대신 SQL 주입을 방지하기 위해'@ LicID'를 사용하는 것이 좋습니다. Value = LicenseNumber''@LicID'를 호출하려고 시도 할 때 작동하지 않습니다. ('@LicID', SqlDbType.Int) myCmd.Parameters ("@ LicID"). 쿼리에서 또한'LicenseNumber'는 txtlicnum이라는 텍스트 상자에서 값을 전역으로 유지하는 String으로 설정된 변수입니다. –

답변

0

원본 코드에 불일치 정의가 있는데 일부 제거 및 변경했습니다.

Private Sub LoadAttachments() 
     attachmentsTable = New DataTable 

     Dim mAdapter As New SqlDataAdapter 
     If LicenseNumber IsNot Nothing Then 
      If Not String.IsNullOrEmpty(InsCommonLib.Settings.MSSqlConStr) Then 
       Dim query As String = "SELECT DocType, docyear, CASE WHEN docmonth IS NULL THEN NULL " & _ 
          "WHEN docmonth = '0' THEN '- All Months -' WHEN docmonth >= 1 AND docmonth <= 12 " & _ 
          "THEN DATENAME(month, DATEADD(month, docmonth, -1)) END DocMonth, DID from dbo.Document where XALASKAID = @LICNUM and DOCTYPE like '%Report%'" 

       Using mConn As New SqlConnection(InsCommonLib.Settings.MSSqlConStr) 
        Using mCmd As New SqlCommand(query, mConn) 
         mCmd.Parameters.Add(New SqlParameter("@LICNUM", LicenseNumber)) 
         mConn.Open() 
         mAdapter.SelectCommand = mCmd 
         mAdapter.Fill(attachmentsTable) 
        End Using 
       End Using 
      End If 

     End If 

     DataGridView3.DataSource = attachmentsTable 
     Label4.Text = "Found " & DataGridView3.Rows.Count & " images" 
    End Sub 

도움과 시간에 감사드립니다.