1

Suppose I have a myTest.sql scripts, which contains thousands of "create table blahblah" statements.

myTest.sql :

 CREATE TABLE A;
 CREATE TABLE A1;
 ....
 CREATE TABLE A1000;

What Im trying to achieve is that make an C# script to force MySql server EXECUTE the myTest.sql file, instead of doing

  using (MySqlConnection cn = new MySqlConnection(ConectionString))
  {
    MySqlCommand newCmd = new MySqlCommand("create statement here 1", cn);
    cn.Open();
    newCmd.ExecuteNonQuery();
    cn.Close();
  }     

I dont want to repeat 1000 times or a for loop something like that. Thanks for all helps and please forgive my grammar problems.

3 Answers 3

2

Could you load the myTest.sql file into a string and pass it to the MySqlCommand.

string myTestSql = IO.File.ReadAllText("myTest.sql");
...
MySqlCommand newCmd = new MySqlCommand(myTestSql, cn);

Should work as long as MySQL accepts commands separated by semicolons.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx Andy ! Which one should I use ? ExecuteNonQuery?
0

You certainly don't have to open and close the connection every single time, but it would be the cleanest if you ran each of these one at a time and look at the result to ensure that the statement completed successfully. Unfortunately if you run a giant statement with 1000 statements and it fails, you don't have an easy way of determining which step(s) were successful and which have to be repeated.

1 Comment

Thx scwagner ! Thats true .. But right now I dont have a better solution, besides table creatation statements .. I got other hundreds of create stored procedure statements ..
0

Use this
You can do it using mysql command-line and shell with System.Process static methods if you want to use .net / c#.

3 Comments

Thx Tocco. But I am not sure MySql Command-line is a mandatory install option.. I cant guarantee that every user installed mysql command-line
I shouldn't know that is a Client application. Did you try to use @AndyP approach?
Not yet .. wondering how to run "newCmd", like calling ExecuteNonQuery or the other..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.