本实例使用VB语言和OLEDB类连接SQL Server 2005数据库。
在sheet1上右键→查看代码,加入如下代码:
Sub admin()Dim conn, xRsSet conn = CreateObject("ADODB.Connection")conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PWD=abc;Initial Catalog=GoodLuck;Data Source=localhost"Set xRs = CreateObject("ADODB.RecordSet")sSql = " Select * from View_S_E"xRs.Open sSql, conn, 1, 3Sheet1.Cells.ClearSheet1.Range("A1").CopyFromRecordset xRsxRs.Closeconn.CloseSet xRs = NothingSet conn = Nothing
End Sub