通過asp程序實現批量access數據庫導入到sql server數據庫中
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
比如access數據庫中有a,b,c三個字段,總共有10條數據,可直接通過asp程序一次性批量導入到sql server數據庫中 方法一:先將access數據庫中的字段綁定到datagrid再將字段插入sql server數據庫 設conn1連接ACCESS,conn2連接SQLServer,在SQLServer數據庫中已建好與ACCESS數據庫相同表名 set rs=conn1.execute "select * from 表名" do while not(rs.eof or rs.bof) conn2.execute "insert into 表名(a,b,c) values("&rs("a")&","&rs("b")&","&rs("c")) rs.movenext loop 方法二:沒辦法直接導入,但是可以先建立與access數據庫連接 從access表中讀出數據,存在一個數據集里,然后再建立與sql數據庫的連接,把數據集里的數據一條條插入到sql的目標表中 當然,這個表必須事先建立 要注意的是兩個表之間如果字段類型不同,在插入數據時要注意轉換類型。 函數
Function ImportData(strTableName,objConnSource,objConnDestination) dim nFieldCount, iLoop dim rsSource dim rsDestination dim strSQL dim strFuncMessage set rsSource = CreateObject("ADODB.RecordSet") set rsDestination = CreateObject("ADODB.RecordSet") strFuncMessage = "" strSQL = "select * from " & strTableName on error resume next rsSource.open strSQL, objConnSource,adOpenStatic,adLockOptimistic if err.number <> 0 then ImportData = -1 exit function end if if objConnSource.Errors.Count > 0 then strFuncMessage = strFuncMessage & "<br>Message: [" & strTableName & "] Not found!" rsSource.Close set rsSource = nothing ImportData = -1 else strMessage = strMessage & "<br>Message:Table[" & strTableName & "] found! " end if nFieldCount = rsSource.Fields.Count redim preserve arrFieldNameList(nFieldCount) For iLoop = 0 to nFieldCount - 1 arrFieldNameList(iLoop) = rsSource.Fields(iLoop).Name Next strSQL = "delete from " & strTableName objConnDestination.Execute(strSQL) strFuncMessage = strFuncMessage & "<br>Message: Delete [" & strTableName & "]" rsDestination.Open strTableName, objConn,adOpenStatic,adLockPessimistic, adCmdTable Do while not rsSource.Eof rsDestination.AddNew For iLoop = 0 to nFieldCount-1 rsDestination(arrFieldNameList(iLoop)) = rsSource(arrFieldNameList(iLoop)) Next RsDestination.Update rsSource.MoveNext Loop rsSource.Close set rsSource = nothing rsDestination.Close strSQL = "select product_id from " & strTableName rsDestination.Open strSQL, objConnSource,adOpenStatic,adLockOptimistic iLoop = rsDestination.RecordCount rsDestination.Close set rsDestination = nothing ImportData = iLoop end function 該文章在 2012/1/31 9:05:21 編輯過 |
關鍵字查詢
相關文章
正在查詢... |