I dont know how to do multiple queries. They all have parameters
Main Topics
Browse All TopicsHi,
I am currently using MySqlCommand to insert records into a remote database. It's very slow executing one insert query at a time.
Can somebody show me how I could do multiple inserts per query to speed it up?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
For example
object[] data = {new object[]{1,"One"}, new object[]{2,"Two"}};
// initiate command
MySqlCommand myCommand = new MySqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.Connection = new MySqlConnection(myConnectS
StringBuilder sql = new StringBuilder();
for(int i=0; i<data.Length; i++)
{
object[] item = (object[])data;
// build the command with multiple insert statements
if(sql.Length > 0) sql.Append(";"); //separate by ;
sql.Append("insert into atable(a,b) value(@a").Append(i).Appen
// set parameter values
myCommand.Parameters.Add("
myCommand.Parameters.Add("
}
// execute the statements
myCommand.CommandText = sql.ToString();
myCommand.ExecuteNonQuery(
The final result of the sql var should be
insert into atable(a,b) values(@a1,@b1);insert into atable(a,b) values(@a2,@b2)
(Sorry, it's missing a 's' after word 'value' in my code above)
There are 2 insert statements in *one* query as zattz requested, they are separated by ';' in the query.
What I need to do next is set parameter values of @a1, @a2, @b1, @b2.
Or am I missing something?
I know it's a closed post but my solution might help others more than the solution above
Use the mysql way. The syntax would be:
INSERT INTO x (a,b)
VALUES
('1', 'one'),
('2', 'two'),
('3', 'three')
Just keep in mind that statement might get bigger than the Max Allowed Packet size in MySQL. So keep that in mind when using.
Business Accounts
Answer for Membership
by: gnoonPosted on 2007-09-01 at 06:50:35ID: 19813816
Please try to separate them with ;