Quantcast
Channel: MySQL Forums - Connector/NET and C#, Mono, .Net
Viewing all articles
Browse latest Browse all 1451

VB.NET LOAD DATA Query With SET @ Statements (no replies)

$
0
0
I am attempting to execute a LOAD DATA statement in a VB.NET application and I receive a fatal error during the try. Digging deeper into the error, I get an error stating that the @InvoiceDate parameter is not set. The query that is built in the application executes without issue in the MySQL Query Browser.

I am using the .net connector, version 6.4.4

Code and built query below:

strSQL = ""
strSQL = strSQL + "LOAD DATA LOCAL INFILE '" + sPolicyStatementsImportFile + "' REPLACE "
strSQL = strSQL + "INTO TABLE `gny`.`policybillstatements` "
strSQL = strSQL + "FIELDS TERMINATED BY '~' "
strSQL = strSQL + "OPTIONALLY ENCLOSED BY '" + Chr(34) + "' "
strSQL = strSQL + "LINES TERMINATED BY '\n' "
strSQL = strSQL + "(PolicyNumber,@InvoiceDate,@PolicyEffDate,@PolicyExpDate,@DueDate,PolicyType,InsuredName,"
strSQL = strSQL + "BillToName1,BillToName2,BillToAddress1,BillToAddress2,BillToCity,BillToState,BillToZipCode,"
strSQL = strSQL + "AgentNumber,AgentName1,AgentName2,AgentAddress1,AgentAddress2,AgentCity,AgentState,AgentZipCode,"
strSQL = strSQL + "Description1,@Description1a,Description2,@Description2a,Description3,@Description3a,"
strSQL = strSQL + "PaymentPlan1,Premium1,InstallmentCharge1,MinimumDue1,"
strSQL = strSQL + "PaymentPlan2,Premium2,InstallmentCharge2,MinimumDue2,"
strSQL = strSQL + "PriorBalance,TotalDue) "
strSQL = strSQL + "SET InvoiceDate=STR_TO_DATE(CONCAT(substr(@InvoiceDate,1,4),',',substr(@InvoiceDate,5,2),',',substr(@InvoiceDate,7,2)),'%Y,%m,%d'),"
strSQL = strSQL + "PolicyEffDate=STR_TO_DATE(CONCAT(substr(@PolicyEffDate,1,4),',',substr(@PolicyEffDate,5,2),',',substr(@PolicyEffDate,7,2)),'%Y,%m,%d'),"
strSQL = strSQL + "PolicyExpDate=STR_TO_DATE(CONCAT(substr(@PolicyExpDate,1,4),',',substr(@PolicyExpDate,5,2),',',substr(@PolicyExpDate,7,2)),'%Y,%m,%d'),"
strSQL = strSQL + "DueDate=STR_TO_DATE(CONCAT(substr(@DueDate,1,4),',',substr(@DueDate,5,2),',',substr(@DueDate,7,2)),'%Y,%m,%d'),"
strSQL = strSQL + "Description1a=IF(TRIM(@Description1a)='0',NULL,IF(TRIM(@Description1a)='',NULL,STR_TO_DATE(CONCAT(substr(@Description1a,1,4),',',substr(@Description1a,5,2),',',substr(@Description1a,7,2)),'%Y,%m,%d'))),"
strSQL = strSQL + "Description2a=IF (TRIM(@Description2a)='0',NULL,IF (TRIM(@Description2a)='',NULL,STR_TO_DATE(CONCAT(substr(@Description2a,1,4),',',substr(@Description2a,5,2),',',substr(@Description2a,7,2)),'%Y,%m,%d'))),"
strSQL = strSQL + "Description3a=IF (TRIM(@Description3a)='0',NULL,IF (TRIM(@Description3a)='',NULL,STR_TO_DATE(CONCAT(substr(@Description3a,1,4),',',substr(@Description3a,5,2),',',substr(@Description3a,7,2)),'%Y,%m,%d')))"
strSQL = strSQL + ";"

sMySqlConnectionString = MYSQLConnectionKJH.MySQLConnectString()
Try
Using MySqlConnectionObject As New MySqlConnection(sMySqlConnectionString)
Dim mySelectQuery As String = strSQL
Dim myCommand As New MySqlCommand(mySelectQuery)
myCommand.CommandTimeout = 60
myCommand.Connection = MySqlConnectionObject
myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
myCommand.Connection.Dispose()
End Using
Catch exError As MySqlException
MsgBox("Insert of PolicyStatements records did not work!", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Policy Bill Statements Insert Failed")
MsgBox("MySQL error details." + vbCrLf + vbCrLf + " Error Code - " + CStr(exError.ErrorCode) + "." + vbCrLf + vbCrLf + " " + Trim(exError.Message), MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Policy Bill Statements Insert MySQL Error")
End Try

strSQL would be as follows (which works in MySQL Query Browser):

LOAD DATA LOCAL INFILE 'C:/TestFiles/DWXA500OCSV.TXT'
REPLACE INTO TABLE `gny`.`policybillstatements`
FIELDS TERMINATED BY '~'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(PolicyNumber,@InvoiceDate,@PolicyEffDate,@PolicyExpDate,@DueDate,PolicyType,InsuredName,
BillToName1,BillToName2,BillToAddress1,BillToAddress2,BillToCity,BillToState,BillToZipCode,
AgentNumber,AgentName1,AgentName2,AgentAddress1,AgentAddress2,AgentCity,AgentState,AgentZipCode,
Description1,@Description1a,Description2,@Description2a,Description3,@Description3a,
PaymentPlan1,Premium1,InstallmentCharge1,MinimumDue1,
PaymentPlan2,Premium2,InstallmentCharge2,MinimumDue2,
PriorBalance,TotalDue)
SET InvoiceDate=STR_TO_DATE(CONCAT(substr(@InvoiceDate,1,4),',',substr(@InvoiceDate,5,2),',',substr(@InvoiceDate,7,2)),'%Y,%m,%d'),
PolicyEffDate=STR_TO_DATE(CONCAT(substr(@PolicyEffDate,1,4),',',substr(@PolicyEffDate,5,2),',',substr(@PolicyEffDate,7,2)),'%Y,%m,%d'),
PolicyExpDate=STR_TO_DATE(CONCAT(substr(@PolicyExpDate,1,4),',',substr(@PolicyExpDate,5,2),',',substr(@PolicyExpDate,7,2)),'%Y,%m,%d'),
DueDate=STR_TO_DATE(CONCAT(substr(@DueDate,1,4),',',substr(@DueDate,5,2),',',substr(@DueDate,7,2)),'%Y,%m,%d'),
Description1a=IF(TRIM(@Description1a)='0',NULL,IF(TRIM(@Description1a)='',NULL,STR_TO_DATE(CONCAT(substr(@Description1a,1,4),',',substr(@Description1a,5,2),',',substr(@Description1a,7,2)),'%Y,%m,%d'))),
Description2a=IF (TRIM(@Description2a)='0',NULL,IF (TRIM(@Description2a)='',NULL,STR_TO_DATE(CONCAT(substr(@Description2a,1,4),',',substr(@Description2a,5,2),',',substr(@Description2a,7,2)),'%Y,%m,%d'))),
Description3a=IF (TRIM(@Description3a)='0',NULL,IF (TRIM(@Description3a)='',NULL,STR_TO_DATE(CONCAT(substr(@Description3a,1,4),',',substr(@Description3a,5,2),',',substr(@Description3a,7,2)),'%Y,%m,%d')))
;

Viewing all articles
Browse latest Browse all 1451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>