Reputation: 47
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.
Upvotes: 1
Views: 2402
Reputation: 1705
Use this
You can do it using mysql command-line and shell with System.Process
static methods if you want to use .net / c#.
Upvotes: 0
Reputation: 36
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.
Upvotes: 2
Reputation: 4005
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.
Upvotes: 0