M
MikeC
I have developed a fully functioning sub that parses data from a CSV file
and writes new records to a table in a SQL Server 2000 database.
I am currently in the process of optimizing my code and was wondering if
there is a more efficient/advanced way to parse the data before I pass it to
my SQL string variable, called strSQL.
Would it be faster to use the Split() function to parse each line (using a
comma delimiter) and pass the parsed data to an array? Some other approach?
If so, what would be the most efficient way to do this?
Below is a code *fragment* from my sub. The CSV file has already been
opened for input. An ADO connection has also been opened. All variables
are explicitly declared. Data types are indicated in the variable names.
'Parse strSingleLine at each comma starting at intPosition 1 and moving
'to each new intPosition across each record.
strSearch = ","
'Loop until end of CSV file.
Do While Not EOF(intFileNo)
'Read line into variable. Automatically inputs next line with each
loop.
Line Input #intFileNo, strSingleLine
'Reset intPosition = 1 when starting each record.
intPosition = 1
'Set variable value.
strISOCode = Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition))
'Increment intPosition.
intPosition = InStr(intPosition, strSingleLine, strSearch) + 1
'Set variable value. This variable is not used.
strCurrName = Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition))
'Increment intPosition.
intPosition = InStr(intPosition, strSingleLine, strSearch) + 1
'Set variable value.
dblUSDPerUnit = CDbl(Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition)))
'Increment intPosition. This variable is not currently used, but
may be later.
intPosition = InStr(intPosition, strSingleLine, strSearch) + 1
'Set variable value.
dblUnitsPerUSD = CDbl(Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition)))
'Increment intPosition, but do *not* add 1 since 5 columns are
delimited by 4 commas.
intPosition = InStr(intPosition, strSingleLine, strSearch)
'Set variable value.
dtQuoteDate = CDate(Right(strSingleLine, (Len(strSingleLine) -
intPosition)))
strSQL = "INSERT INTO [w$fxrateload] (batchnumber, from_currency,
to_currency," _
& " Rate, DateValue, from_curr_id, to_curr_id, dateid,"
_
& " executionstamp, Status)" _
& " VALUES (" & intNextBatchNbr & ", '" & strISOCode &
"'" _
& ", 'USD', " & dblUnitsPerUSD & ", '" & dtQuoteDate &
"', " & 0 _
& ", " & 0 & ", " & 0 & ", '" & Now() & "', " & 0 & ")"
cnn1.Execute strSQL
Loop
and writes new records to a table in a SQL Server 2000 database.
I am currently in the process of optimizing my code and was wondering if
there is a more efficient/advanced way to parse the data before I pass it to
my SQL string variable, called strSQL.
Would it be faster to use the Split() function to parse each line (using a
comma delimiter) and pass the parsed data to an array? Some other approach?
If so, what would be the most efficient way to do this?
Below is a code *fragment* from my sub. The CSV file has already been
opened for input. An ADO connection has also been opened. All variables
are explicitly declared. Data types are indicated in the variable names.
'Parse strSingleLine at each comma starting at intPosition 1 and moving
'to each new intPosition across each record.
strSearch = ","
'Loop until end of CSV file.
Do While Not EOF(intFileNo)
'Read line into variable. Automatically inputs next line with each
loop.
Line Input #intFileNo, strSingleLine
'Reset intPosition = 1 when starting each record.
intPosition = 1
'Set variable value.
strISOCode = Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition))
'Increment intPosition.
intPosition = InStr(intPosition, strSingleLine, strSearch) + 1
'Set variable value. This variable is not used.
strCurrName = Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition))
'Increment intPosition.
intPosition = InStr(intPosition, strSingleLine, strSearch) + 1
'Set variable value.
dblUSDPerUnit = CDbl(Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition)))
'Increment intPosition. This variable is not currently used, but
may be later.
intPosition = InStr(intPosition, strSingleLine, strSearch) + 1
'Set variable value.
dblUnitsPerUSD = CDbl(Mid(strSingleLine, intPosition, _
(InStr(intPosition, strSingleLine, strSearch) -
intPosition)))
'Increment intPosition, but do *not* add 1 since 5 columns are
delimited by 4 commas.
intPosition = InStr(intPosition, strSingleLine, strSearch)
'Set variable value.
dtQuoteDate = CDate(Right(strSingleLine, (Len(strSingleLine) -
intPosition)))
strSQL = "INSERT INTO [w$fxrateload] (batchnumber, from_currency,
to_currency," _
& " Rate, DateValue, from_curr_id, to_curr_id, dateid,"
_
& " executionstamp, Status)" _
& " VALUES (" & intNextBatchNbr & ", '" & strISOCode &
"'" _
& ", 'USD', " & dblUnitsPerUSD & ", '" & dtQuoteDate &
"', " & 0 _
& ", " & 0 & ", " & 0 & ", '" & Now() & "', " & 0 & ")"
cnn1.Execute strSQL
Loop