sql >> Database teknologi >  >> RDS >> Sqlserver

Brug Excel 2010 til at læse/skrive til en SQL Server 2008-database ved hjælp af lagrede procedurer

Jeg endte med at gå med denne tilgang ved hjælp af AD-godkendelse. Jeg brugte eksemplet i dette indlæg til inspiration:http://www.eggheadcafe.com/community/sql-server/13/10141669/using-excel-to-update-data-on-ms-sql-tables .aspx

Bemærk, at disse funktioner findes i forskellige områder af Excel-projektmappen (objekter, moduler, denne projektmappe), men her er en hurtig reference.

Jeg har også hver af de kolonner, der FK validerer mod de tabeller, de refererer til.

Her er nogle kodeeksempler:

Public aCon As New ADODB.Connection
Public aCmd As New ADODB.Command

Private Sub Workbook_Open()
  Application.EnableEvents = False
  PopulateSheet
  Application.EnableEvents = True
End Sub

Sub Connect()
  Dim sConn As String
  sConn = "Provider=SQLOLEDB;Trusted_Connection=Yes;Server=[SVR];Database=[DB]"
  With aCon
    .ConnectionString = sConn
    .CursorLocation = adUseClient
    .Open
  End With
  BuildProcs
End Sub

Sub BuildProcs()
  With aCmd
    .ActiveConnection = aCon
    .CommandType = adCmdStoredProc
    .CommandText = "[SPROC]"
    .Parameters.Append .CreateParameter("@in_EmployeeID", adInteger, adParamInput)
  End With

End Sub

Sub PopulateSheet()
  Dim n As Integer, r As Long

  Dim aCmdFetchEmployees As New ADODB.Command
  Dim aRstEmployees As New ADODB.Recordset

  If aCon.State = adStateClosed Then Connect

  With aCmdFetchEmployees
    .ActiveConnection = aCon
    .CommandType = adCmdStoredProc
    .CommandText = "[SPROC]"
    Set aRstEmployees = .Execute
  End With
  r = aRstEmployees.RecordCount

  Worksheets(1).Activate
  Application.ScreenUpdating = False
  Cells(2, 1).CopyFromRecordset aRstEmployees

  For n = 1 To aRstEmployees.Fields.Count
    Cells(1, n) = aRstEmployees(n - 1).Name
    Cells(1, n).EntireColumn.AutoFit
  Next
  Cells(1).EntireColumn.Hidden = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim cell As Range
  If aCon.State = adStateClosed Then Connect
    With aCmd
      .Parameters(0) = Cells(Target.Row, 1)
      .Parameters(1) = Cells(Target.Row, 4)
      .Parameters(2) = Cells(Target.Row, 5)
      .Parameters(3) = Cells(Target.Row, 6)
      .Parameters(4) = Cells(Target.Row, 7)
      .Parameters(5) = Cells(Target.Row, 8)
      .Parameters(6) = Cells(Target.Row, 10)
      .Parameters(7) = Cells(Target.Row, 11)
      .Parameters(8) = Cells(Target.Row, 12)
      .Parameters(9) = Cells(Target.Row, 13)
      .Parameters(10) = Cells(Target.Row, 14)
      .Parameters(11) = Cells(Target.Row, 15)
      .Parameters(12) = Cells(Target.Row, 16)
      .Execute , , adExecuteNoRecords
    End With

End Sub


  1. Gør dine databasekomponenter meget tilgængelige (HA) via Load Balancers

  2. Start eller stop mysql-database ved hjælp af java-programmering

  3. Brug af MySQL IN-klausul som alt inklusive (AND i stedet for OR)

  4. Hvilket python 3-bibliotek skal jeg bruge til MySQL?