2

I want to execute all sql files in folders. I found the answer in this question is helpful. I can use their command and it works as expect.

I want to run it from C# code. i tried a couple ways but I couldn't get the result. Below is the code that I have tried but not success.

Process process = new Process();
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = string.Format("for %f in ({2}/*.sql) do sqlcmd /S {0} /d {1} /U {3} /P {4} /E /i {5}",
sqlServerName, databaseName, folder, sqlUserName, sqlPassword, @"""%f""");
process.StartInfo.WorkingDirectory = @"C:\";
process.Start();
process.WaitForExit();

I want to know how can I move this DOS command for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" into C# code

Community
  • 1
  • 1
Anonymous
  • 9,366
  • 22
  • 83
  • 133

3 Answers3

2

If you are using C# you should use the tools available to you in C#. Sql Server has a SDK that is included with the install of Sql Server.

You will need to add at least Microsoft.SqlServer.Smo.dll and Microsoft.SqlServer.ConnectionInfo.dll to use the following code.

ServerConnection connection = new ServerConnection(sqlServerName, sqlUserName, sqlPassword);
Server server = new Server(connection);
Database database = server.Databases[databaseName];

foreach (var sqlFile in Directory.EnumerateFiles(folder, "*.sql"))
{
    //Parses the file and runs the batches
    database.ExecuteNonQuery(File.ReadAllText(sqlFile));
}

This will read each file, break the file apart the batches (GO statements), then run the queries.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1

This code may help...

var SqlFile = @"...";
var Command = new SqlCommand
{
  CommandType = CommandType.Text,
  Connection = new SqlConnection()
};
Command.Connection.Open();
foreach (var CommandText in File.ReadAllText(SqlFile).Replace("GO", ";").Split(';'))
{
  Command.CommandText = CommandText;
  Command.ExecuteNonQuery();
}
Command.Connection.Close();
Amir
  • 770
  • 8
  • 21
  • +1 for doing it the right way, but can you post some more of a explanation of why this way is better. – Scott Chamberlain Oct 03 '12 at 05:33
  • :( Removing the +1 due to the fact that the line `Select 'What if the script contains a ; or a GO inside it?'` would bomb your code. – Scott Chamberlain Oct 03 '12 at 05:35
  • When ; is used we eliminate a command but as you said GO is the problem so we should check new line character(s) before and after that (OR) use ReadAllLines and replace lines with GO with ; – Amir Oct 03 '12 at 05:52
0

If you want to run all commands together then the easy way is create batch file
and use this -

Process myProcess = new Process();
myProcess = Process.Start(@"Test.bat");

But if you want to run some command then some C# code then again some Command and so on
then u will need multiple batch file and that will be messy way.

USER_NAME
  • 1,029
  • 1
  • 14
  • 33