在http://dev.mysql.com/doc/refman/5.5/en/connector-net-programming-prepared.html的示例代码中,有代码(每次都做:cmd.ExecuteNonQuery()
在迭代中):
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO myTable VALUES(NULL, @number, @text)";
cmd.Prepare();
cmd.Parameters.AddWithValue("@number", 1);
cmd.Parameters.AddWithValue("@text", "One");
for (int i=1; i <= 1000; i++)
{
cmd.Parameters["@number"].Value = i;
cmd.Parameters["@text"].Value = "A string value";
cmd.ExecuteNonQuery();
}
}
为什么不是(做1次:cmd.ExecuteNonQuery()
迭代后):
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO myTable VALUES(NULL, @number, @text)";
cmd.Prepare();
for (int i=1; i <= 1000; i++)
{
cmd.Parameters.AddWithValue("@number", i);
cmd.Parameters.AddWithValue("@text", "One");
}
cmd.ExecuteNonQuery();
}