0

SQLインジェクションを回避する方法がわかりません。誰かが私の問題を手伝ってくれるでしょうか。

これが私の現在のコードです:

Private Function INSERT() As String
        Dim SQLcon As New SqlConnection
        Dim SQLdr As SqlDataReader
        Try
            SQLcon.ConnectionString = "Data Source=#####;Initial Catalog=OJT;Persist Security Info=True;User ID=####;Password=#####"
            Dim SQLcmd As New SqlCommand("INSERT INTO dbo.Patients(pIDNo,pLName,pFName,pMI,pSex,pStatus,pTelNo,pDocID,pAddr,pStreet,pBarangay,pCity,pProvince,pLNameKIN,pFNameKIN,pMIKIN,pRelationKIN) VALUES('" & LabelPNumber.Text & "','" & txtLname.Text & "','" & txtFname.Text & "','" & txtMI.Text & "','" & txtPatientSex.Text & "','" & txtPatientSex.Text & "','" & txtPatientTelNo.Text & "','" & txtPatientDoctor.Text & "','" & txtStreetNumber.Text & "','" & txtStreetName.Text & "','" & txtBarangay.Text & "','" & txtCity.Text & "','" & txtProvince.Text & "','" & txtKinLname.Text & "','" & txtKinFname.Text & "','" & txtKinMI.Text & "','" & txtRelationToPatient.Text & "') ", SQLcon)
            SQLcon.Open()
            MsgBox("Patient Added!", MsgBoxStyle.Information)
            SQLdr = SQLcmd.ExecuteReader()
        Catch ex As Exception
            MessageBox.Show("Error Occured, Can't Add Patient!" & ex.Message)
        Finally
            SQLcon.Close()
        End Try
        Return "done"
    End Function
4

1 に答える 1

4

基本的に、文字列を連結してSQLステートメントを作成する場所、特にユーザー入力からのステートメントは脆弱です。

これを行う代わりに、SQLコマンドのParametersプロパティに追加できるSQLパラメーターを使用します(SQLcmdここ)。

パラメータの1つを使用した例を示します-SQLCommandテキストを次のように変更します。

INSERT INTO dbo.Patients(pIDNo, ...)
VALUES(@pIDNo, ...)

SQLParametersコレクション@pIDNoのコマンドとは別に送信されるパラメータ値の文字列の「プレースホルダー」はどこにありますか。

次に、この「プレースホルダー」と同じ名前のパラメーターと値を追加できます(指定された値からタイプが派生します)。

これが前の例です:

SQLcmd.Parameters.AddWithValue("@pIDNo", LabelPNumber.Text)
于 2013-02-02T10:45:03.863 に答える