0

Possible Duplicate:
How to execute an .SQL script file using c#

How would I programmatically run a SQL script that's in a file on the local disk?

Community
  • 1
  • 1
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336

1 Answers1

1

Answer taken from here How to execute an .SQL script file using c#

Put the command to execute the sql script into a batch file then run the below code

string batchFileName = @"c:\batosql.bat";
string sqlFileName = @"c:\MySqlScripts.sql";
Process proc = new Process();
proc.StartInfo.FileName = batchFileName;
proc.StartInfo.Arguments = sqlFileName;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.ErrorDialog = false;
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileName);
proc.Start();
proc.WaitForExit();
if ( proc.ExitCode!= 0 )

in the batch file write something like this (sample for sql server)

osql -E -i %1
Community
  • 1
  • 1
Rama
  • 429
  • 1
  • 8
  • 14