本帖最后由 作者 于 2004-10-16 11:53:38 编辑
在发表此帖之前,我先要感谢辉兄的帮助!
陈四清 chen4@py.gov.cn
在作城市地理测绘信息工作处理时要用到大量的坐标数据记录和基础情报资料(一般是成千上万条记录),这些资料现都是用Access数据库存放处理的,在作CAD成图处理时,本人太过愚钵,只会autolisp语言,且只会读写TXT文本文件,所以每次成图前不得不多次转换,麻烦而易出差错,在明经上看到郑先生<< VLISP应用技巧Visual LISP中使用ADO接口与MS-Access相连接>>的一文,很感兴趣,但试验了多次,老出错误,前几天,在辉兄的关照下,
给你一段程序,你看看,建议从主函数开始理解(C:TEST),用你给我的数据库,注意路径为E:/AAA.MDB,
(defun DbInitADO ( / ADO_DLLPath) (if (null adom-Append) (progn (setq ADO_DLLPath (strcat (getenv "systemdrive") "\\Program Files\\Common Files\\System\\Ado\\") ) ;; 如果查找到类型库 ... (if (findfile (strcat ADO_DLLPath "msado15.dll")) ;; 将其输入 (vlax-Import-Type-Library :tlb-filename (strcat ADO_DLLPath "msado15.dll") :methods-prefix "adom-" :properties-prefix "adop-" :constants-prefix "adok-" ) ;; 找不到时,则通知操作者 (alert (strcat "不能找到以下文件\n" ADO_DLLPath "msado15.dll")) ) ) ) )
;生成MS-Access 或 MS-SQL Server 数据库的连接字符串 ;;;****************************************************************** ;;; 使用ODBC(不需要DSN)连接MS-Access数据库 ;;; 示例: (DbConnect_MSAccess1 "d:/dbfiles/products.mdb") ;;;******************************************************************
(defun DbConnect_MSAccess1 (dbFile) (strcat "Provider=MSDASQL;" "Driver={Microsoft Access Driver (*.mdb)};" "DBQ=" dbFile ) )
;从内存中释放VLA对象 (defun MxRelease (xObject) vlax-object-released-p (if (not (vlax-object-released-p xObject)) (vlax-Release-Object xObject) ) )
;关闭ADO Connection 对象并将内存释放出来 (defun DbCloseConnection (dbConnObject) (vlax-Invoke-Method dbConnObject "Close") (MxRelease dbConnObject) )
;关闭ADO RecordSet对象并将内存释放出来 (defun DbCloseRecordset (rsObject) (vlax-Invoke-Method rsObject "Close") (MxRelease rsObject) )
;布尔测试RecordSet 是否为 Closed (T 或 nil) (defun DbRsIsClosed (rsObject) (= adok-adStateClosed (vlax-Get-Property rsObject "State")) )
;返回一个ADO RecordSet对象中的记录数 (defun DbRsCount (rsObject) (vlax-Get-Property rsObject "RecordCount") )
;返回Field对象中给定字段数的字段名称 (defun DbGetFields (fObject fCount / FieldNumber) (setq FieldNumber -1) (while (> fCount (setq FieldNumber (1+ FieldNumber))) (setq FieldList (cons (vlax-Get-Property (DbRsFieldItem FieldsObject FieldNumber) "Name") FieldList)); setq ); end while ); defun
;从RecordSet对象返回ADO Field对象 (defun DbRsFields (rsObject) (vlax-Get-Property rsObject "Fields") )
;返回给定Field对象的字段数量 (defun DbRsFieldCount (fObject) (vlax-Get-Property fObject "Count") )
;获取Field对象的字段名(项) (defun DbRsFieldItem (fObject fNumber) (vlax-Get-Property fObject "Item" fNumber) )
;返回RecordSet对象的RowSet对象 (defun DbRsGetRows (rsObject) (vlax-Invoke-Method rsObject "GetRows" adok-adGetRowsRest) )
;应用一个ADO LOCK(锁定)类型到给定的RecordSet对象 (defun DbRsLockType (rsObject lockType) (cond ( (= (strcase lockType) "OPTIMISTIC") (vlax-Put-Property rsObject "LockType" adok-adLockOptimistic) ) ( (= (strcase lockType) "BATCHOPTIMISTIC") (vlax-Put-Property rsObject "LockType" adok-adLockBatchOptimistic) ) ( (= (strcase lockType) "READONLY") (vlax-Put-Property rsObject "LockType" adok-adLockReadOnly) ) ) )
(defun Search(RSObject / lst FieldsObject FieldCount FieldList ReturnValue) (defun getlst (var) (setq lst nil) (setq n (length var) i 0) (while (< i n) (setq lst (append lst (list (vlax-variant-value (nth i var))))) (setq i (1+ i)) ) lst ) (setq FieldsObject (DbRsFields RSObject) ;; 将字段作为对象 FieldCount (DbRsFieldCount FieldsObject) ;; 取得列的数量 FieldList (DbGetFields FieldsObject FieldCount) ;; 取得列表中所有列的名称 ReturnValue (list (reverse FieldList))) ; setq (setq lst (vlax-safearray->list (vlax-variant-value (dbrsgetrows rsobject)))) (setq lst (mapcar 'getlst lst)) (setq lst (mapcar '(lambda(var1 var2) (append (list var1) (list var2))) (car returnvalue) lst)) (print lst) )
;A sample... (defun c:test (/ getlst dbconnection rsobject SqlString) (DbInitADO) ;初始化 (setq DBConnection (vlax-create-object "adodb.connection")) ;创建并返回ADO Connection对象 (setq RSObject (vlax-create-object "adodb.recordset")) ;创建并返回ADO RecordSet对象 (setq SqlString "select 点号,横坐标,纵坐标,井盖高 from pspoint") (vlax-invoke-method DBConnection "open" (DbConnect_MSAccess1 "e:\\AAA.mdb") "admin" "" adok-adConnectUnspecified) (vlax-invoke-method RSObject "open" SqlString DBConnection nil nil adok-adcmdtext) (search rsobject) (DbCloseRecordset rsobject) (DbCloseConnection dbconnection) (princ) )
meflying@mjtd.com 2004-10-10
(经过辉兄的提醒,所述错误仅是一种表象,为减少网友的阅读负担,现删除太长的错误传述,而保留正确的程序,供各位网友使用)!
|