2009-11-06 9 views
4

Excel 문제 : 사용자가 단추를 클릭하면 VBA가 입력 파일을 구문 분석하여 스프레드 시트의 셀에 데이터를 저장합니다. 그런 다음 스프레드 시트 사본을 데이터 작업자에게 우편으로 보냅니다.SSIS가없는 SQL Server에 Excel VBA

SQL Server에서 데이터를 표시하는 SSRS 또는 ASP 또는 Sharepoint로 바꾸겠습니다.

현재 프로세스를 방해하지 않고 작업하려면 Excel VBA를 스프레드 시트에 행을 쓸 때마다 저장 프로 시저를 통해 SQL Server DB에 삽입하고 싶습니다.

나중에 SSIS 가져 오기를 위해 CSV 행을 파일에 쓰지 만 직접 DB로 보내야합니다.

내가 VB.Net에서 작업을 수행하는 방법을 알고 있지만 내가 VBA에서 데이터를 작성하지 한 (자주 는 레코드에 데이터를 읽을 수는 있지만 기록되지).

저장 프로 시저에 매개 변수로 값을 전달하는 것을 선호하지만 필요한 경우 각 행에 대해 느린 INSERT 명령을 생성 할 수 있습니다.

답변

5

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 
+0

감사합니다. 아마 결국 그걸 알아 냈 겠지만 문법 오류를 얻는 데 많은 시간을 절약하고, 다시보고, 다시 시도해 보았습니다. 사람이 데이터를 입력하지 않아 SQL 주입이 걱정되지 않습니다. 이전 VBA는 Medicare에서 데이터 파일을 구문 분석하여 가져옵니다. 또한 데이터 유형은 달러, 날짜 및 숫자입니다. SQL을 사용하기가 어렵습니다! – WGroleau

0

VBA로 데이터를 어떻게 읽습니까?

ADO 레코드 집합을 사용하는 경우 : ADODB.Command 클래스를 살펴보십시오. 이를 통해 SQL 또는 저장 프로 시저를 실행하고 매개 변수를 전달할 수 있습니다 (Google ado 명령 예제).

DAO 레코드 세트를 사용하는 경우 : DAO 데이터베이스의 Execute 메서드를 사용하면 SQL 문을 실행할 수 있습니다.

+0

예, VBA로 읽지는 않지만 작성했습니다. – WGroleau

0

결국 사람들은 마침내 더 나은 방법을 받아들이 기 시작했습니다. 버튼 클릭이 아닌 자동화는 파일을 DB (SSIS)로 직접 읽으며 데이터가 필요한 사람들은 전자 메일 대신 보고서를 봅니다. 우편 엑셀 파일.