How to execute an .SQL script file using c#

后端 未结 10 898
忘了有多久
忘了有多久 2020-11-22 17:03

I\'m sure this question has been answered already, however I was unable to find an answer using the search tool.

Using c# I\'d like to run a .sql file. The sql file

10条回答
  •  情深已故
    2020-11-22 17:52

    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
    

提交回复
热议问题