S
shapper
Hello,
Is it possible to run a SQL script from a C# Console application?
Thanks,
Miguel
Is it possible to run a SQL script from a C# Console application?
Thanks,
Miguel
Sure, there isn't any reason why the choice of user interface wouldshapper said:Hello,
Is it possible to run a SQL script from a C# Console application?
Yeah, you make an sproc that contains the script and execute the sproc
in the console application using ADO.NET SQL Command object.
I assume you mean you have a file containing the SQL. If this is the case,
read the content of the file as a string, replace to GO statements with a
";". Execute the string.
However, not all sql statements can be executed in a batch, so you may want
to split the string to create an array. Loop through the array then executing
each statement.
something like...
string text = System.IO.File.ReadAllText("myscript.sql");
text = text.Replace("GO", ";");
string[] statements = text.Split(Char.Parse(";"));
for(int i = 0; i < statements.Length ;i++)
{
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand(statements, conn)
conn.Open();
cmd.ExecuteNonQuery();
}
NOTE: "GO" and ";" in comments will present a problem.