I have a datatable with some rows I need to insert that datatable in to database using a stored procedure.
For Each row As DataRow In dt.Rows
cmd.Parameters.Add(New MySqlParameter("@DOWNLOAD_ID", dt.Rows(0)("DOWNLOAD_ID").ToString()))
cmd.Parameters.Add(New MySqlParameter("@FINALY_FIELDCODE", dt.Rows(0)("FINALY_FIELDCODE").ToString().Replace("|", "").ToString()))
cmd.Parameters.Add(New MySqlParameter("@UNITS", dt.Rows(0)("UNITS").ToString()))
cmd.Parameters.Add(New MySqlParameter("@CURRECY_CODE", dt.Rows(0)("CURRENCY_CODE").ToString()))
Next
cmd.ExecuteNonQuery()
When i do this I got an exception {"Parameter '@DOWNLOAD_ID' has already been defined."}
How to get rid of this exception and can we insert the datatable with out for loop. Can any one help me
You should create separate MySqlCommands and execute them directly:
Using mysqlConnection = New MySqlConnection(connectionString)
mysqlConnection.Open()
For Each row As DataRow In dt.Rows
Using cmd = New MySqlCommand("StoredProcedureName", mysqlConnection)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New MySqlParameter("@DOWNLOAD_ID", dt.Rows(0)("DOWNLOAD_ID").ToString()))
cmd.Parameters.Add(New MySqlParameter("@FINALY_FIELDCODE", dt.Rows(0)("FINALY_FIELDCODE").ToString().Replace("|", "").ToString()))
cmd.Parameters.Add(New MySqlParameter("@UNITS", dt.Rows(0)("UNITS").ToString()))
cmd.Parameters.Add(New MySqlParameter("@CURRECY_CODE", dt.Rows(0)("CURRENCY_CODE").ToString()))
cmd.ExecuteNonQuery()
End Using
Next
End Using
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.