通過asp程序?qū)崿F(xiàn)批量access數(shù)據(jù)庫導(dǎo)入到sql server數(shù)據(jù)庫中
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
比如access數(shù)據(jù)庫中有a,b,c三個(gè)字段,總共有10條數(shù)據(jù),可直接通過asp程序一次性批量導(dǎo)入到sql server數(shù)據(jù)庫中 方法一:先將access數(shù)據(jù)庫中的字段綁定到datagrid再將字段插入sql server數(shù)據(jù)庫 設(shè)conn1連接ACCESS,conn2連接SQLServer,在SQLServer數(shù)據(jù)庫中已建好與ACCESS數(shù)據(jù)庫相同表名 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 方法二:沒辦法直接導(dǎo)入,但是可以先建立與access數(shù)據(jù)庫連接 從access表中讀出數(shù)據(jù),存在一個(gè)數(shù)據(jù)集里,然后再建立與sql數(shù)據(jù)庫的連接,把數(shù)據(jù)集里的數(shù)據(jù)一條條插入到sql的目標(biāo)表中 當(dāng)然,這個(gè)表必須事先建立 要注意的是兩個(gè)表之間如果字段類型不同,在插入數(shù)據(jù)時(shí)要注意轉(zhuǎn)換類型。 函數(shù)
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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |