uo_xls_backup.sru 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. $PBExportHeader$uo_xls_backup.sru
  2. forward
  3. global type uo_xls_backup from nonvisualobject
  4. end type
  5. end forward
  6. global type uo_xls_backup from nonvisualobject
  7. end type
  8. global uo_xls_backup uo_xls_backup
  9. type variables
  10. transaction xls_trans
  11. private transaction acc_trans
  12. uo_win32api uo_api
  13. uo_xls_constant uo_const
  14. end variables
  15. forward prototypes
  16. public function integer createaccessdb (string arg_filedir, string arg_filename, boolean arg_ifkeepconnect, ref string arg_msg)
  17. private function string create_export_string (string arg_filepath, string arg_uid, string arg_pwd, string arg_tablename, string arg_accesstablename)
  18. public function transaction create_access_transaction (boolean arg_autocommit, string arg_uid, string arg_pwd, string arg_defaultdir, string arg_filename, boolean arg_autoconnect, ref string arg_msg)
  19. public function integer exporttoaccess (string arg_filedir, string arg_filename, boolean arg_ifcommit, ref string arg_msg)
  20. private function string create_import_string (string arg_filepath, string arg_uid, string arg_pwd, string arg_tablename, string arg_accesstablename)
  21. public function integer importfromacess (string arg_filedir, string arg_filename, boolean arg_ifcommit, ref string arg_msg)
  22. public function integer createaccesstable (boolean arg_ifcommit, boolean arg_ifkeepconnect, ref string arg_msg)
  23. public function integer createtable (boolean arg_ifcommit, ref string arg_msg)
  24. end prototypes
  25. public function integer createaccessdb (string arg_filedir, string arg_filename, boolean arg_ifkeepconnect, ref string arg_msg);String filepath = ''
  26. //long rslt = 1
  27. filepath = arg_filedir+'\'+arg_filename
  28. IF FileExists(filepath) THEN
  29. IF MessageBox('系统提示' , '数据库‘' + filepath + ' ’已经存在,是否覆盖?' , question! , yesno! , 2) = 1 THEN
  30. IF NOT FileDelete(filepath) THEN
  31. arg_msg = '替换文件'+arg_filename+'失败,'+arg_filename+'可能正在被使用'
  32. RETURN 0
  33. END IF
  34. ELSE
  35. RETURN 2
  36. END IF
  37. END IF
  38. OLEObject appAccess
  39. appAccess = CREATE OLEObject
  40. IF appAccess.ConnectToNewObject("Access.Application") <> 0 THEN
  41. arg_msg = 'OLE无法连接!'
  42. RETURN 0
  43. END IF
  44. appAccess.Visible = FALSE
  45. appAccess.NewCurrentDatabase(filepath)
  46. appAccess.quit()
  47. appAccess.DisconnectObject()
  48. DESTROY appAccess
  49. acc_trans = create_access_transaction(FALSE,'admin','',arg_filedir,arg_filename,TRUE,arg_msg)
  50. IF IsNull(acc_trans) THEN
  51. IF NOT FileDelete(filepath) THEN
  52. arg_msg += '~n删除临时文件'+arg_filename+'失败,'+arg_filename+'可能正在被使用,请手动删除'
  53. RETURN 0
  54. END IF
  55. RETURN 0
  56. END IF
  57. IF createaccesstable(TRUE , arg_ifkeepconnect,arg_msg) <> 1 THEN
  58. DISCONNECT USING acc_trans ;
  59. DESTROY acc_trans
  60. SetNull(acc_trans)
  61. IF NOT FileDelete(filepath) THEN
  62. arg_msg += '~n删除临时文件'+arg_filename+'失败,'+arg_filename+'可能正在被使用,请手动删除'
  63. RETURN 0
  64. END IF
  65. RETURN 0
  66. END IF
  67. //ext:
  68. IF NOT arg_ifkeepconnect THEN
  69. DISCONNECT USING acc_trans ;
  70. DESTROY acc_trans
  71. END IF
  72. SetNull(acc_trans)
  73. RETURN 1
  74. end function
  75. private function string create_export_string (string arg_filepath, string arg_uid, string arg_pwd, string arg_tablename, string arg_accesstablename);string str
  76. str = "insert into openrowset('Microsoft.Jet.OLEDB.4.0', '"+arg_filepath+"';'"+arg_uid+"';'"+arg_pwd+"','SELECT * FROM "+arg_accesstablename+"') SELECT * FROM "+arg_tablename
  77. return str
  78. end function
  79. public function transaction create_access_transaction (boolean arg_autocommit, string arg_uid, string arg_pwd, string arg_defaultdir, string arg_filename, boolean arg_autoconnect, ref string arg_msg);transaction acc
  80. IF NOT FileExists(arg_DefaultDir + "\" + arg_filename) THEN
  81. arg_msg = '目标数据库文件:'+arg_DefaultDir + "\" + arg_filename +'不存在'
  82. SetNull(acc)
  83. GOTO ext
  84. END IF
  85. acc = CREATE Transaction
  86. acc.DBMS = "ODBC"
  87. acc.AutoCommit = arg_AutoCommit
  88. acc.DBParm = "ConnectString='DRIVER=Driver do Microsoft Access (*.mdb);" + &
  89. "UID="+arg_uid+";" + &
  90. "PWD="+arg_pwd+";" + &
  91. "UserCommitSync=Yes;"+ &
  92. "Threads=3;"+ &
  93. "SafeTransactions=0;"+ &
  94. "PageTimeout=5;"+ &
  95. "MaxScanRows=8;"+ &
  96. "MaxBufferSize=2048;"+ &
  97. "FIL=MS Access;"+ &
  98. "DriverId=25;"+ &
  99. "DefaultDir= " + arg_DefaultDir + ";"+ &
  100. "DBQ=" + arg_DefaultDir + "\" + arg_filename
  101. CONNECT USING acc ;
  102. IF acc.SQLCode <> 0 THEN
  103. arg_msg = acc.SQLErrText
  104. DESTROY acc
  105. SetNull(acc)
  106. ELSE
  107. IF NOT arg_autoconnect THEN
  108. DISCONNECT USING acc;
  109. END IF
  110. END IF
  111. ext:
  112. RETURN acc
  113. end function
  114. public function integer exporttoaccess (string arg_filedir, string arg_filename, boolean arg_ifcommit, ref string arg_msg);long rslt
  115. rslt = createaccessdb(arg_filedir,arg_filename,false,arg_msg)
  116. if rslt <> 1 then
  117. return rslt
  118. end if
  119. string exporttr[]
  120. long i
  121. xls_trans.autocommit = true
  122. EXECUTE IMMEDIATE 'SET ANSI_NULLS ON' using xls_trans;
  123. EXECUTE IMMEDIATE 'SET ANSI_WARNINGS ON' using xls_trans;
  124. for i = 1 to upperbound(uo_const.xls_tablelist )
  125. exporttr[i] = create_export_string(arg_filedir+'\'+arg_filename,'admin','',uo_const.xls_tablelist[i],uo_const.xls_tablelist[i])
  126. EXECUTE IMMEDIATE :exporttr[i] USING xls_trans;
  127. IF xls_trans.SQLCode <> 0 THEN
  128. arg_msg = "导出表"+uo_const.xls_tablelist[i]+"时错误:"+xls_trans.SQLErrText+'~n' + exporttr[i]
  129. rollback using xls_trans;
  130. xls_trans.autocommit = false
  131. RETURN 0
  132. END IF
  133. next
  134. xls_trans.autocommit = false
  135. if arg_ifcommit then
  136. commit using xls_trans;
  137. end if
  138. return 1
  139. end function
  140. private function string create_import_string (string arg_filepath, string arg_uid, string arg_pwd, string arg_tablename, string arg_accesstablename);string str
  141. str = "insert into "+arg_tablename+" SELECT * from openrowset('Microsoft.Jet.OLEDB.4.0', '"+arg_filepath+"';'"+arg_uid+"';'"+arg_pwd+"','SELECT * FROM "+arg_accesstablename+"') "
  142. return str
  143. end function
  144. public function integer importfromacess (string arg_filedir, string arg_filename, boolean arg_ifcommit, ref string arg_msg);string exporttr[]
  145. long i
  146. i = createtable(false , arg_msg)
  147. IF i <> 1 THEN
  148. rollback using xls_trans;
  149. RETURN i
  150. END IF
  151. EXECUTE IMMEDIATE 'SET ANSI_NULLS ON' using xls_trans;
  152. EXECUTE IMMEDIATE 'SET ANSI_WARNINGS ON' using xls_trans;
  153. for i = 1 to upperbound(uo_const.xls_tablelist )
  154. exporttr[i] = create_import_string(arg_filedir+'\'+arg_filename,'admin','',uo_const.xls_tablelist[i],uo_const.xls_tablelist[i])
  155. EXECUTE IMMEDIATE :exporttr[i] USING xls_trans;
  156. IF xls_trans.SQLCode <> 0 THEN
  157. arg_msg = "导入表"+uo_const.xls_tablelist[i]+"时错误:"+xls_trans.SQLErrText+'~n' + exporttr[i]
  158. rollback using xls_trans;
  159. RETURN 0
  160. END IF
  161. next
  162. if arg_ifcommit then
  163. commit using xls_trans;
  164. end if
  165. return 1
  166. end function
  167. public function integer createaccesstable (boolean arg_ifcommit, boolean arg_ifkeepconnect, ref string arg_msg);
  168. SetPointer(hourglass!)
  169. Integer i , i_row
  170. //执行语句
  171. i_row = UpperBound(uo_const.acc_create_str )
  172. FOR i = 1 TO i_row
  173. EXECUTE IMMEDIATE :uo_const.acc_create_str[i] USING acc_trans;
  174. IF acc_trans.SQLCode <> 0 THEN
  175. arg_msg = "创建表时错误:"+acc_trans.SQLErrText+'~n' + uo_const.acc_create_str[i]
  176. rollback using acc_trans;
  177. RETURN 0
  178. END IF
  179. NEXT
  180. if arg_ifcommit then
  181. COMMIT USING acc_trans ;
  182. end if
  183. if not arg_ifkeepconnect then
  184. DISCONNECT USING acc_trans ;
  185. end if
  186. //CONNECT USING acc_trans;
  187. Yield()
  188. //创建成功
  189. //DISCONNECT USING acc_trans ;
  190. //DESTROY acc_trans
  191. RETURN 1
  192. end function
  193. public function integer createtable (boolean arg_ifcommit, ref string arg_msg);
  194. SetPointer(hourglass!)
  195. Integer i , i_row
  196. //执行语句
  197. i_row = UpperBound(uo_const.xls_drop_str )
  198. FOR i = 1 TO i_row
  199. EXECUTE IMMEDIATE :uo_const.xls_drop_str[i] USING xls_trans;
  200. NEXT
  201. i_row = UpperBound(uo_const.xls_create_str )
  202. FOR i = 1 TO i_row
  203. EXECUTE IMMEDIATE :uo_const.xls_create_str[i] USING xls_trans;
  204. IF xls_trans.SQLCode <> 0 THEN
  205. arg_msg = "创建表时错误:"+xls_trans.SQLErrText+'~n' + uo_const.xls_create_str[i]
  206. rollback using xls_trans;
  207. RETURN 0
  208. END IF
  209. NEXT
  210. if arg_ifcommit then
  211. COMMIT USING xls_trans ;
  212. end if
  213. Yield()
  214. RETURN 1
  215. end function
  216. on uo_xls_backup.create
  217. call super::create
  218. TriggerEvent( this, "constructor" )
  219. end on
  220. on uo_xls_backup.destroy
  221. TriggerEvent( this, "destructor" )
  222. call super::destroy
  223. end on
  224. event constructor;uo_api = create uo_win32api
  225. uo_const = create uo_xls_constant
  226. end event