VBA에서 가장 쉬운 데이터 액세스 라이브러리는 ADO입니다. ADODB. * 개체를 사용할 수 있도록 "Microsoft ActiveX 데이터 개체 라이브러리"에 대한 참조를 추가하십시오.
는 (귀하의 경우 테이블에 레코드를 추가합니다) 저장 프로 시저를 실행하려면, 당신은 그것을 할 수 :
... 게으른 방법 (파라미터 객체를 사용하지 않고 직접 SQL 문을 생성하는 단계;이) SQL 인젝션 해킹하는 경향이있다 :
Public Sub AddFoo _
(_
strServer As String, _
strDatabase As String, _
strUsername As String, _
strPassword As String, _
lFooValue As Long _
)
' Build the connection string
Dim strConnectionString As String
strConnectionString = "Driver={SQL Server}" _
& ";Server=" & strServer _
& ";Database=" & strDatabase _
& ";UID=" & strUsername _
& ";PWD=" & strPassword
' Create & open the connection
Dim oConnection As Connection
Set oConnection = New Connection
oConnection.ConnectionString = strConnectionString
oConnection.Open
' Build the SQL to execute the stored procedure
Dim strSQL As String
strSQL = "EXEC AddFoo " & lFooValue
' Call the stored procedure
Dim oCommand As Command
Set oCommand = New Command
oCommand.CommandType = adCmdText
oCommand.CommandText = strSQL
oCommand.ActiveConnection = oConnection
oCommand.Execute
oConnection.Close
End Sub
... 또는 모든 파라미터의 부호화 다루고, 따라서 SQL 인젝션 해킹하는 경향이없는 정확한 방법 (- 의도적 또는 우발적)
Public Sub AddFoo _
(_
strServer As String, _
strDatabase As String, _
strUsername As String, _
strPassword As String, _
lFooValue As Long _
)
' Build the connection string
Dim strConnectionString As String
strConnectionString = "Driver={SQL Server}" _
& ";Server=" & strServer _
& ";Database=" & strDatabase _
& ";UID=" & strUsername _
& ";PWD=" & strPassword
' Create & open the connection
Dim oConnection As Connection
Set oConnection = New Connection
oConnection.ConnectionString = strConnectionString
oConnection.Open
' Build the SQL to execute the stored procedure
Dim strSQL As String
strSQL = "EXEC AddFoo " & lFooValue
' Create the command object
Dim oCommand As Command
Set oCommand = New Command
oCommand.CommandType = adCmdStoredProc
oCommand.CommandText = "AddFoo"
' Create the parameter
Dim oParameter As Parameter
Set oParameter = oCommand.CreateParameter("foo", adParamInteger, adParamInput)
oParameter.Value = lFooValue
oCommand.Parameters.Add oParameter
' Execute the command
oCommand.ActiveConnection = oConnection
oCommand.Execute
oConnection.Close
End Sub
감사합니다. 아마 결국 그걸 알아 냈 겠지만 문법 오류를 얻는 데 많은 시간을 절약하고, 다시보고, 다시 시도해 보았습니다. 사람이 데이터를 입력하지 않아 SQL 주입이 걱정되지 않습니다. 이전 VBA는 Medicare에서 데이터 파일을 구문 분석하여 가져옵니다. 또한 데이터 유형은 달러, 날짜 및 숫자입니다. SQL을 사용하기가 어렵습니다! – WGroleau