|
通过三层结构模型远程访问ACCESS数据库(3) --------------在原来的基础上加了一层---------------- 客户端接口采用VB中的类来实现,程序中使用了3个类,CConnection, CRecordset, CTcpIp,其中CTcpIp是关于访问网络方面的,与用户基本上没有直接关系,和用户有关的是CConnection,CRecordset这2个类,它们分别相当于ADO中ADODB.Connection和ADODB.Recordset。有了这2个接口类,就可以用来实现大部分的数据库操作,具体用法如下:
1.定义对象 Dim conn As New CConnection 'ADODB.Connection Dim rs As New CRecordset 'ADODB.Recordset 2.打开服务器端数据库 Dim bConn As Boolean bConn = conn.OpenConnection("192.168.1.155", "DSN=testmdb;DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;PWD=;") If Not bConn Then MsgBox conn.GetLastError() Set conn = Nothing Exit sub End If
3.1.打开服务器上的记录集,并获取数据 Set rs = conn.OpenResultset("SELECT * FROM zdxx") If rs Is Nothing Then MsgBox "记录集合打开错误!" Exit Sub End If
'得到总的字段数 nFieldsCound = rs.GetFieldsCount() Do Until rs.EOF sField1 = rs.GetFieldValueByIndex(0) ’第一个字段的下标从0开始 sField2 = rs.GetFieldValueByIndex(1) sField3 = rs.GetFieldValueByIndex(2) Debug.Print sField1, sField2, sField3 rs.MoveNext Loop rs.CloseRecordset set rs = nothing 3.2.更新数据库 Dim nRowAffected As Long dim strSQL as String strSQL = "INSERT into zdxx(zdxx,lkd,tabstop) values('测试内容','120','1')" '可以是INSERT,UPDATE,DELETE中的任何一种 bOK = conn.Execute(strSQL, nRowAffected) If (Not bOK) Then MsgBox conn.GetLastError
|