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);
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);
相关文章推荐
- MySQL执行外部sql脚本文件的命令
- C#读取*.sql文件,并执行里面的SQL语句
- C#执行一个目录下所有sql文件
- c#中调用cmd命令执行批处理文件
- mssql用cmd命令执行*.sql文件
- sqlplus 命令 执行整个目录的所有文件
- C#执行sql文件
- 【python】MySQLdb执行sql文件命令
- MySQL执行外部sql脚本文件的命令
- Linux下的Mysql,导入执行sql文件命令
- C# 中直接执行sql文件
- MySQL执行外部sql脚本文件的命令
- C# 调用并执行SQL脚本文件
- MYSQL 手动执行sql脚本文件的命令
- C#保留2位小数几种场景总结 游标遍历所有数据库循环执行修改数据库的sql命令 原生js轮盘抽奖实例分析(幸运大转盘抽奖) javascript中的typeof和类型判断
- C# 调用并执行SQL脚本文件
- MySQL用source命令执行外部sql脚本文件时出现failed to open file的错误可能之一
- MySQL执行外部sql脚本文件的命令
- C#执行SQL脚本文件
- 如何用C#程序直接执行.sql文件里的SQL语句