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 |
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 SQLAllocConnect(giHEnv,giHDB)<>SQL_SUCCESS Then MsgBox "SQL Server couldn注釋:t connect!" End If myConnection="DSN=myServer;UID=LCL;PWD=;APP=ODBCTest;WSID=LCL;DATABASE=sales" myResult=SQLDriverConnect(giHDB,Test,form1.hWnd,myConnection.len(myConnection), myBuff,256,myBufflen,SQL_DRIVER_COMPLETE_REQU IE D) myResult=SQLAllocStmt(giHDS,giHStmt) myResult=SQLFreeStmt(giHStmt,SQL_COLSE) rsSQL="Select * from Customers Where City = "Hunan"" myResult = SQLExecDirect(giHStmt,rsSQL,Len(rsSQL)) |
Private Sub InitializeApplication() DBLIB_VERSION=SqlInit() If DBLIB_VERSION=""Then MsgBox"Could not initialize DBLIB!Exit application.", MB_ICONEXCLAMATION End If End Sub Private Function LoginToServer() As integer loginToServer=SUCCEED Status%=SqlSetloginTime%(loginTimeOut) If giSqlConn<>0 Then SqlClose(giSqlConn) 注釋:關閉已打開的連接 giSqlConn=SqlOpenConnection(gsServerName, gsLoginID, gsPassword, ProgramName, ProgramName) If giSqlConn<>0 Then liresuit=SqlUse(giSqlConn,"Sales") Else LogintoServer=FAIL End If End Function |
Dim Cn As rdoConnection Dim En As rdoEnvironment Dim Conn As String Conn = "DSN = MyDSN; UID = Jacob;" & "PWD = 123456; DATA BASE = MyDb;" Set Cn= En.OpenConnection("", rdDriverPrompt, False, Co nn) Set Cn= En.OpenConnection(Prompt:= rdDriverPrompt, Rea dOnly:= False,Connect:= Cnn) |
Public WithEvents Eng As rdoEngine Public WithEvents Cn As rdoConnection Private Sub Form_Load() Set Eng = New rdoEngine Set Cn = New rdoConnection With Cn .Connect = "UID = ; PWD = ;" & "DATABASE = pubs; DSN = biblio" .LoginTimeout = 5 .EstablishConnection rdoDriverNoPromt, True, rdAsyncEna ble End With End Sub |
Private Sub Cn_BeforeConnect(ConnetString As String, Pro mpt As Variant) MsgBox "正在連接" & ConnectString, vbOKOnly, "連接前" End Sub |
Private Sub Cn_Connect(ByVal ErrorOccurred As Boolean) Dim M As String If ErrorOccurred Then For Each er In rdoErrors M = M & er & vbCrLf & M Next MsgBox "連接失敗" & vbCrLf & M Else MsgBox "連接成功" 注釋:這是確認連接狀態的測試代碼 Cn.Excute "use pubs" End Sub |
Private Sub Eng_InfoMessage() For Each er In rdoErrors Debug.Print er Next RdoErrors.Clear End Sub |
Cn.Close Set Cn = Nothing 注釋:釋放對象所占的內存資源 En.Close Set En = Nothing 注釋:釋放對象所占的內存資源 |
VB是對象語言,Form、ActiveX控件也都是對象。使用對象后必須養成將對象設為Nothing把它從內存中釋放的編程習慣。這樣可以預防很多不可預測錯誤,往往程序中發生原因不明的錯誤時,其原因就在于此?! ?
5、ADO 數據對象(Active Data Objects)
ADO是基于全新的OLE DB技術,OLE DB可對電子郵件、文本文件、復合文件、數據表等各種各樣的數據通過統一的接口進行存取。隨著ActiveX控件的升級(Windows 98的ActiveX 5.0),RDO將被以ActiveX技術為基礎的ADO接口所替代。下面將介紹基于ActiveX技術的ADO訪問SQL Server 6.5數據庫的技術和方法?;跒g覽器的ADO接口常用函數如下:
(1)取當前的工作數據庫
由于管理任務一般都必須在Master庫中完成,因此在執行管理任務之前,最好保存當前工作庫,以便完成任務之后再切換回原來的任務?! ?
Public Function SQLGetCurrentDatabaseName(Cn As ADODB.Connection) As String Dim sSQL As String Dim RS As New ADODB.Recordset On Error GoTo errSQLGetCurrentDatabaseName sSQL="select CurrentDB=DB_NAME ( )" RS.Open sSQL, Cn SQLGetCurrentDatabaseName=Trim $ (RS! CurrentDB) RS.Close Exit Function errSQLGetCurrentDatabaseName: SQLGetCurrentDatabaseName=" " End Function |
Public Function SQLGetDataPath(Cn As ADODB.Connection) As String Dim sSQL As String Dim RS As New ADODB.Recordset Dim sFullPath As String On Error GoTo errSQLGetDataPath sSQL="select phyname from master..sysdevices where name=注釋:master注釋: " RS.Open sSQL, Cn sFullPath = RS! phyname RS.Close SQLGetDataPath=Left $ (sFullPath, Len(sFullPath) -10) 注釋:MASTER.DAT的大小 Exit Function errSQLGetDataPath: SQLGetDataPath=" " End Function |
Public Function SQLCreateDatabase65 (Cn As ADODB.Connection,sDBName As String, sDataDeviceName As String, nDataSize As Integer, Optional sLogDeviceName, Optional nLogSize) As Boolean Dim sSQL As String On Error GoTo errSQLCreateDatabase65 Dim sDB As String sDB =SQLGetCurrentDatabaseName(Cn) sSQL = "USE master" Cn.Execute sSQL sSQL ="CREATE DATABASE" & sDBName sSQL = sSQL &" ON " & sDataDeviceName & "=" & nDataSize If Not IsMissing(sLogDeviceName) And Not IsMissing(nLogSize) Then sSQL = sSQL & "LOG ON" & sLogDeviceName & "="& nLogSize End If Cn.Execute sSQL sSQL = "USE" & sDB Cn.Execute sSQL SQLCreateDatabase65 = True Exit Function errSQLCreateDatabase65: On Error Resume Next sSQL = "USE " & sDB Cn.Execute sSQL SQLCreateDatabase65 = False End Function |
Public Function SQLExistDatabase(Cn As ADODB.Connection, sDBName As String) As Boolean Dim sSQL As String Dim RS As New ADODB.Recordset Dim bTmp As Boolean on Error GoTo errSQLExistDatabase sSQL = "select CntDB = count ( * ) " sSQL = sSQL & "From master.dbo.sysdatabases" sSQL = sSQL & "Where name = 注釋: "& sDBName & " 注釋: " RS.Open sSQL, Cn If RS! CntDB = 0 Then bTmp = False Else bTmp = True RS.Close SQLExistDatabase = bTmp Exit Function errSQLExistDatabase: SQLExistDatabase = False Exit Function End Function |
Public Function SQLDropDatabase (Cn As ADODB.Connection, sDBName As String) As Boolean Dim sSQL As String On Error GoTo errSQLDropDatabase If Not SQLExistDatabase(Cn, sDBName) Then SQLDropDatabase = True Exit Function End If Dim sDB As String sDB = SQLGetCurrentDatabaseName(Cn) sSQL = "Use master" Cn.Execute sSQL sSQL = "DROP DATABASE " & sDBName Cn.Execute sSQL sSQL = "USE " & sDB Cn.Execute sSQL SQLDropDatabase = True Exit Function errSQLDropDatabase: On Error Resume Next sSQL = "USE " & sDB Cn.Execute sSQL SQLDropDatabase = False End Function |
原文轉自:http://www.anti-gravitydesign.com