'some BCP constants
Private Const DBIN& = 1
' transfer from client to server
Private Const DBOUT& = 2
' transfer from server to client
Private Const SUCCEED& = 1
Private Const FAIL& = 0
Private Declare Function SqlBCPSetL Lib "VBSQL.OCX" _
(ByVal loginrec As Long, ByVal Enable As Long) As Long
Private Declare Function SqlLogin Lib "VBSQL.OCX" _
() As Long
Private Declare Function SQLSETLUSER Lib "VBSQL.OCX" _
Alias "SqlSetLUser" (ByVal Login As Long, User As _
String) As Long
Private Declare Function SqlSetLPwd Lib "VBSQL.OCX" _
(ByVal Login As Long, Pwd As String) As Long
Private Declare Function SqlSetLApp Lib "VBSQL.OCX" _
(ByVal Login As Long, App As String) As Long
Private Declare Function SqlOpen Lib "VBSQL.OCX" _
(ByVal Login As Long, Server As String) As Long
Private Declare Function SqlBCPInit Lib "VBSQL.OCX" _
(ByVal SqlConn As Long, TblName As String, _
HFile As String, ErrFile As String, ByVal _
Direction As Integer) As Long
Private Declare Function SqlBCPColfmt Lib "VBSQL.OCX" _
(ByVal SqlConn As Long, ByVal FColumn As Long, _
ByVal FType As Long, ByVal FPLen As Long, _
ByVal FCLen As Long, FTerm As String, _
ByVal FTLen As Long, ByVal TCol As Long) As Long
Private Declare Function SqlBCPColumns Lib "VBSQL.OCX" _
(ByVal SqlConn As Long, ByVal ColCount As Long) _
As Long
Private Declare Function SqlBCPExec Lib "VBSQL.OCX" _
(ByVal SqlConn As Long, RowsCopied As Long) As Long
Private Declare Sub SqlClose Lib "VBSQL.OCX" _
(ByVal SqlConn As Long)
Private Declare Sub SqlExit Lib "VBSQL.OCX" ()
Private Declare Sub SqlWinExit Lib "VBSQL.OCX" ()
Private Sub ExecuteBCP()
'Called by method LoadTable
'Use vbsql.ocx to bcp data from local file to _
SQL Server
Dim loginrec As Long
Dim SqlConn As Long
Dim Result As Long
Dim RowsCopied As Long
Dim sServer As String
Dim i As Integer
Dim sDestTable As String
'Here we use in-line error handling, since BCP _
interface is a C-style interface
On Error GoTo 0
loginrec = SqlLogin
sServer = FindValue(sDestConnectionString, "Server")
Result = SQLSETLUSER(loginrec, _
FindValue(sDestConnectionString, "UID"))
Result = SqlSetLPwd(loginrec, _
FindValue(sDestConnectionString, "PWD"))
Result = SqlSetLApp(loginrec, "DataPump")
Result = SqlBCPSetL(loginrec, True)
SqlConn = SqlOpen(loginrec, sServer)
If SqlConn = 0 Then GoTo BCPOpenError
'get fully-qualified name
sDestTable = FindTableNameInSQL(sDestSelectSQL)
Result = SqlBCPInit(SqlConn, sDestTable, sFileName, _
sBCPErrorFileName, DBIN)
If Result = FAIL Then GoTo BCPInitError
'Execute the bulk-copy.
Result = SqlBCPColumns(SqlConn, iNoOfFields)
For i = 1 To iNoOfFields - 1
Result = SqlBCPColfmt(SqlConn, i, &bF, 0, -1, _
sBCPColumnSeparator, _
Len(sBCPColumnSeparator), i)
Next i
'last one, use vbcrlf as row separator
Result = SqlBCPColfmt(SqlConn, iNoOfFields, &bF, _
0, -1, vbCrLf, 2, iNoOfFields)
Result = SqlBCPExec(SqlConn, RowsCopied)
If Result = FAIL Then GoTo BCPExecuteError
mlNoOfRowsLoaded = RowsCopied
On Error GoTo UnknownError
SqlClose (SqlConn)
SqlExit
SqlWinExit
'delete the bcp data file
If Not bKeepBCPFile Then
Kill sFileName
If sBCPErrorFileName <> " Then
Kill sBCPErrorFileName
End If
End If
Exit Sub
...(Error handling routines)
End Sub
|