62,268
社区成员
发帖
与我相关
我的任务
分享//调用osql执行脚本
private void ExcuteScript()
{
try
{
//数据库登录信息
string _saPassword = this.Context.Parameters["pwd"];
string _dataBaseName = this.Context.Parameters["dbname"];
string _targetPath = this.Context.Parameters["targetdir"];
string _servername = this.Context.Parameters["server"];
string _username = this.Context.Parameters["user"];
Process sqlProcess = new Process();
//调用osql必须在目标机,也就是安装的机子上要有安装SQLServer服务器
//不然找不到这个命令
sqlProcess.StartInfo.FileName = "osql.exe";
//如下所指的SQL脚本文件是打包打安装项目的文件名,
//targetPath就是在安装界面用户指定的安装目录
sqlProcess.StartInfo.Arguments = string.Format("-U {0} -P{1} -d {2} -S {3} -i {4}db.sql",
_username, _saPassword, _dataBaseName, _servername, _targetPath);
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
//删除脚本文件
FileInfo sqlFileInfo = new FileInfo(String.Format("{0}db.sql", this.Context.Parameters["targetdir"]));
if (sqlFileInfo.Exists)
sqlFileInfo.Delete();
}
catch (Exception ex)
{
WriteLog(ex.Message);
throw ex;
}
}