下列程式碼說明了開啟 ADO Recordset (資料錄集) 物件的三種不同方式。其中每種方法都會連接到 Microsoft SQL Server Pubs 資料庫而且可以加入到 Form1 Load 事件中: ' 1. Open Recordset through the Execute method of the Connection object.
cn.Open(cnStr)
rs = cn.Execute("Select * from Authors")
rs.Close()
cn.Close()
' 2. Open Recordset through the Command.Execute method.
cn.Open(cnStr)
cmd.ActiveConnection = cn
cmd.CommandText = "Select * from Authors"
rs = cmd.Execute
rs.Close()
cn.Close()
' 3. Open Recordset without a Connection object.
rs.Open("Select * from Authors", cnStr)
rs.Close()
' Release the objects to free memory.
rs = Nothing
cn = Nothing
|