Form Declarations Dim mydb As Database Dim mydynaset As Dynaset Private Sub Form_Load() Set mydb=OpenDatabase("",False,False,"ODBC;DSN=Myserver;WSID=LCL;DATABASE=sales") Set mydynaset=mydb CreateDynaset("Select*from Customers")
End Sub 上述例子是以非独占、非只读方式打开sales数据库,并检索Customers表中的所有字段。OpenDatabase函数的最后一个参数是ODBC连接字符串参数,它指明了Microsoft Access连接到SQL Server所需要知道的一些内容。其中“DSN”为数据源名,“WSID”为工作站名,“DATABASE”为所要访问的数据库名。
Global giHEnv As Long Global giHDB As Long Global giHStmt As Long Dim myResult As integer Dim myConnection As Srting Dim myBuff As String*256 Dim myBufflen As Integer If SQLAllocEnv(giHEnv)<>SQL_SUCCESS Then MsgBox"Allocation couldn't happen!" End if if SQLllocConnect(giHEnv,giHDB)<>SQL_SUCCESS Then MsgBox "SQL Server couldn't connect!" End if myConnection="DSN=myServer;UID=lcl;PWD=;APP=ODBCTest;WSlD=LCL;DATABASE=sales" myResult=SQLDriverConnect(giHDB,Test,form1.hWnd,myConnection.len(myConnection), myBuff,256,myBufflen,SQL_DRIVER_COMPLETE_REQUIED) myResult=SQLAllocStmt(giHDS,giHStmt) myResult=SQLFreeStmt(giHStmt,SQL_COLSE) rsSQL="Select*from Customers Where City="Wuhan"" myResult=SQLExecDirect(giHStmt,rsSQL,Len(rsSQL))