2013-02-02 6 views
0

SQL 주입을 피하는 방법을 모르겠지만 누군가 내 문제를 해결할 수 있습니까? 특히 사용자의 입력에서 오는 즉, 취약, SQL 문을 만들어 함께Visual Basic 2010의 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 
+0

사용 [파라미터 (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx) – rene

+0

매개 변수 sir rene을 사용하는 SELECT STATEMENT의 예는 무엇입니까? – Danjor

+0

을 다른 동료가 지정한대로 sql-parameters를 사용하십시오. 나는 오늘 당신의 관심사 일 수도있는 [이 다른 질문] (http://stackoverflow.com/a/14660347/1203135)에 대한 샘플을 썼습니다. –

답변

3

기본적으로 어디서든 당신이 합치하고 문자열 :

여기에 내 현재 코드입니다.

이 작업을 수행하는 대신 SQL 명령의 Parameters 속성 (여기 SQLcmd)에 추가 할 수있는 SQL 매개 변수를 사용하십시오.

내가 당신에게 당신의 매개 변수 중 하나를 예를 보여 드리겠습니다 -로하는 SqlCommand 텍스트를 변경 : @pIDNo는 별도로 전송되는 매개 변수 값에 대한 문자열에 "자리"입니다

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

SQLParameters collection의 명령.

그런 다음이 "자리 표시 자"와 이름이 같은 매개 변수를 추가하고 값 (제공된 값에서 유형을 파생시킵니다)을 추가 할 수 있습니다. 여기

이전의 예제 :

SQLcmd.Parameters.AddWithValue("@pIDNo", LabelPNumber.Text) 
+0

SQL 매개 변수를 사용하여이 코드의 일부 예제를 제공 할 수 있습니까? 내 SELECT 문에 SQL 매개 변수가 있지만 INSERT STATEMENT에서 수행하는 방법을 모르겠다. 내게 줄 수 있습니까? – Danjor

+0

@Danjor 당신이 이것을 썼을 때 나는 정확하게 편집하고있었습니다. 제 편집을보십시오. – Bridge

+0

코드 덕분에, 나는 그것을 내 프로그램에 적용 할 것이고, 나는 단지 SQL Injection>을 피하려고한다. 왜냐하면 나는 무엇이 INJECTOR인지 알고 있기 때문이다. – Danjor

관련 문제