VB使用ADO物件連結資料庫

weixin_34402090發表於2011-06-30

 1. 定義連線物件

  Global DBconnect As New ADODB.Connection

  2. 執行連線語句

  If DBconnect.State = adStateOpen And Not IsEmpty(adStateOpen) Then DBconnect.Close

  連線ODBC

  DBconnect.ConnectionString = "dsn=DataAliasName;uid=UserID;pwd=Passwd;"

  直接連線Access

  Dbconnect.Provider = "Microsoft.jet.OLEDB.4.0" // Access 97為3.51

  DBconnect.ConnectionString = "FilePathFileName.mdb"

  連線Oracle

  DBconnect.Provider = "MSADORA"

  DBconnect.ConnectionString = "user/mypass@servicename

  連線SQL Server

  DBconnect.Provider = "SQLOLEDB.1"

  DBconnect.ConnectionString = "DATABASE= ;SERVER= ;UID= ;PWD= ;"

  或者可以使用 DBconnect.Open "SERVER" , "USERID" , "PASSWORD"

  DBConnect.Open

  3. 取查詢結果集

Global RS As New ADODB.Recordset
Global PS As New ADODB.Recordset
If RS.State = adStateOpen And Not IsEmpty(adStateOpen) Then RS.Close
   RS.Open SQLStr, DBconnect, adOpenKeyset

  4. 執行SQL語句

  DBconn.Execute SQLStr

  5. 關閉資料庫

  DBconnect.Close

相關文章