Function Main()
Dim filePath
Dim cnSQL
Set cnSQL = CreateObject("Adodb.Connection")
filePath = "E:\NET3.5代码学习\BI学习\DBF"
'On Error GoTo ERRORHANDLER
cnSQL.ConnectionString = "Provider=SQLOLEDB.1;SERVER=HJ\NCZERG;DATABASE=DTS;UID=sa;PWD=spider*123"
cnSQL.Open
'cnSQL.Open "Provider=SQLOLEDB.1;User ID=sa;password=spider*123;Initial Catalog=DTS;Data Source=."
SearchFolder cnSQL, filePath
'ERRORHANDLER:
cnSQL.Close
Main = DTSTaskExecResult_Success
End Function
' get all dbf files in folder
Sub SearchFolder(cnSQL ,Folder)
dim fso,cnDBF,rsDBF,strDBF
Dim objFile, objFolder
Set cnDBF = CreateObject("Adodb.Connection")
Set rsDBF = CreateObject("Adodb.RecordSet")
set fso=CreateObject("scripting.filesystemobject")
'open this dbf table
strDBF = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB="& Folder &";Exclusive=No;Collate=Machine;NULL=NO"
cnDBF.ConnectionString = strDBF
cnDBF.Open
' get all files in folder
Set objFolder = fso.GetFolder(Folder)
For Each objFile In objFolder.Files
'MsgBox objFile.Path
If (Right(objFile, 4) = ".dbf") Then
ExecuteDBF cnSQL,cnDBF,rsDBF, objFile
End If
Next
cnDBF.Close
'For Each objFolder In objFolder.SubFolders
' SearchFolder objFolder
'Next
End Sub
Public Sub ExecuteDBF(cnSQL,cnDBF,rsDBF,dbfPath)
Dim Conn,ConnStr,rs,tableName,i
'get tablename from filepath
Dim arr
arr = Split(dbfPath,"\")
tableName = arr(Ubound(arr))
rsDBF.Open "select * from " & tableName, cnDBF,1,3
If (rsDBF.RecordCount > 0) Then
For i = 1 To rsDBF.RecordCount
Call ExecuteInsert(cnSQL,rsDBF("acctName"))
rsDBF.MoveNext
Next
End If
rsDBF.Close()
End Sub
' insert
Public Sub ExecuteInsert(cnSQL, userName)
Dim Cmd
Set Cmd = CreateObject("ADODB.Command")
with Cmd
.ActiveConnection = cnSQL 'cnSQL is connection object
.CommandText = "proc_Insert" 'your procedure name
.CommandType = 4 'this is a procedure
.Prepared = true '要求将SQL命令先行编译
.Parameters.append .CreateParameter("@UserName",200,1,50,userName)
.Execute
end with
Set Cmd = Nothing
'adBigInt: 20 ;
'adBinary : 128 ;
'adBoolean: 11 ;
'adChar: 129 ;
'adDBTimeStamp: 135 ;
'adEmpty: 0 ;
'adInteger: 3 ;
'adSmallInt: 2 ;
'adTinyInt: 16 ;
'adVarChar: 200 ;
End Sub
CreateParameter("参数名称",类型,方向,大小)
参许参数值的类型的意义如下:
名称值 整数值 功能
adDBTimeStamp 135 日期时间数据类型
adDecimal 14 十进制整数值
adDouble 5 双精度小数值
adError 10 系统错误信息
AdGUID 72 全域性唯一识别字(Globally unique identifier)
adDispath 9 COM/OLE自动对象(Automation Object)
adInteger 3 4字节有符号整数
adIUnknown 13 COM/OLE对象
adLongVarBinary 205 大型2字节值
adLongVarChar 201 大型字符串值
adLongVarWChar 203 大型未编码字符串
adNumeric 131 十进制整数值
adSingle 4 单精度浮点小数
adSmallInt 2 2字节有符号整数
adTinyInt 16 1字节有符号整数
adUnsignedBigInt 21 8字节无符号整数
adUnsignedInt 19 4字节无符号整数
adUnsignedSmallInt 18 2字节无符号整数
adUnsignedTinyInt 17 1字节无符号整数
adUserDefined 132 用户自定义数据类型
adVariant 12 OLE对象
adVarBinary 204 双字节字符变量值
adVarChar 200 字符变量值
advarchar 202 未编码字符串变量值
adWchar 130 未编码字符串
方向值的意义如下:
名称值 整数值 功能
adParamInput 1 允许数据输入至该参数当中
adParamOutput 2 允许数据输出至该参数当中
adParamInputOutput 3 允许数据输入、输出至该参数当中
adparamReturnValue 4 允许从一子程序中返回数据至该参数当中