I am trying to insert a large list of data in a MySQL table.
Due to performance issues, I'am filling a List<string>
and with this list I generate my command string to do an unique insert with all my rows.
Here is my code:
StringBuilder cmdText = new StringBuilder("INSERT INTO my_Table (ColumnA, ColumnB) VALUES ");
List<string> aux = new List<string>();
using (MySqlConnection conn = new MySqlConnection(connString))
{
foreach (DataRow row in dataTable.Rows)
{
aux.Add(string.Format("('{0}','{1}')", row[0].ToString(), row[1].ToSTring()));
}
cmdText.Append(string.Join(",", aux));
cmdText.Append(";");
conn.Open();
using (MySqlCommand cmd = new MySqlCommand(cmdText.ToString(), conn))
{
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
};
This way, the result of my cmdText, finally is something like this:
INSERT INTO my_Table (ColumnA, ColumnB) VALUES ('Value1','Value2') ('Value3','Value4') ...;
with aproximately 500k rows in teh same insert.
One or more of this rows can contain values starting with '@'
I'm not using parameters ('@parameter'), but when I execute my application,
I get an error like this:
Fatal error encountered during command execution. "Parameter '@GMAIL.COM' must be defined."
@gmail.com is a value, not a parameter. But it still been interpreted like a @parameter.
Anyone knows the way to indicate that this is not a param but a value??