\\ Home Page : Articolo : Stampa
Uso di Excel come front-end di una semplice query in SQL Server
Di cj (del 28/04/2010 @ 17:58:07, in SQL Server, linkato 201527 volte)

Questo che segue è un semplice esempio dell'uso di MS Excel come front-end per una semplice query su MS SQL Server.

Sub ADOExcelSQLServer()

      Dim Cn As ADODB.Connection

      Dim Server_Name As String

      Dim Database_Name As String

      Dim User_ID As String

      Dim Password As String

      Dim SQLStr As String

      Dim rs As ADODB.Recordset

      Set rs = New ADODB.Recordset

      Server_Name = "LAPTOP\SQL_EXPRESS" ' Enter your server name here

      Database_Name = "Northwind" ' Enter your database name here

      User_ID = "" ' enter your user ID here

      Password = "" ' Enter your password here

      SQLStr = "SELECT * FROM Orders" ' Enter your SQL here

      Set Cn = New ADODB.Connection

      Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ ";Uid=" & User_ID & ";Pwd=" & Password & ";" rs.Open SQLStr, Cn, adOpenStatic

      With Worksheets("Sheet1")

           .Range("A2:Z500")

           .ClearContents

           .CopyFromRecordset rs

     End With

     rs.Close

     Set rs = Nothing

     Cn.Close

     Set Cn = Nothing

End Sub