您的位置:首页 > 数据库

C#执行.sql文件中命令

2010-02-02 12:50 337 查看
首先是一个不需要修改的方法,拿来就用

private string ExeCommand(string commandText)
{

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = "cmd.exe";

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.RedirectStandardError = true;

p.StartInfo.CreateNoWindow = true;

string strOutput = null;

try
{

p.Start();

p.StandardInput.WriteLine(commandText);

p.StandardInput.WriteLine("exit");

strOutput = p.StandardOutput.ReadToEnd();

p.WaitForExit();

p.Close();

}

catch (Exception e)
{

strOutput = e.Message;

}

return strOutput;

}

其调用方法就两句话

string sqlQuery = "osql.exe -U" + loginName + " -P" + loginPwd + " -S" + serverIp + " -d" + dbName + " -iyoursql.sql";

string strRst = ExeCommand(sqlQuery);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: