using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Security.AccessControl;
using System.Text;
using Microsoft.Win32;
namespace Commons.Databaxxxxse
{
/// <summary>
/// OSql命令操作函数(可用于安装程序的时候数据库脚本执行)
/// </summary>
public class SqlscxxxxriptHelper
{
#region OSql操作函数
/// <summary>
/// 本地执行SQL脚本
/// </summary>
/// <param name="path">脚本文件路径全名</param>
public static void DoSQL(string path)
{
string arguments = String.Format(" -E -S (local) -i \"{0}\"" path);
RunDos("" arguments false);
}
/// <summary>
/// 执行SQL脚本
/// </summary>
/// <param name="path">脚本文件路径全名</param>
/// <param name="userID">数据库登录ID</param>
/// <param name="password">数据库登录密码</param>
/// <param name="server">数据库服务器地址</param>
public static void DoSQL(string path string userID string password string server)
{
// -U -P -S -i关键字区分大小写
param name string arguments = String.Format(" -U {0} -P {1} -S {2} -i \"{3}\"" userID password server path);
RunDos("" arguments false);
// if(File.Exists(path))
// {
// File.Delete(path);
// }
}
/// <summary>
/// 后台执行DOS文件
/// </summary>
/// <param name="fileName">文件名(包含路径)</param>
/// <param name="argument">运行参数</param>
/// <param name="hidden">是否隐藏窗口</param>
public static void RunDos(string fileName string argument bool hidden)
{
Process process = new Process();
process.EnableRaisingEvents = false;
process.StartInfo.FileName = string.Format("\"{0}\"" fileName);
process.StartInfo.Arguments = argument;
if (hidden)
{
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
else
{
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
}
process.Start();
process.WaitForExit();
}
/// <summary>
/// 运行指定DOS命令行
/// </summary>
/// <param name="cmd">命令</param>
/// <param name="tempFolder">命令行参数</param>
/// <param name="comfirm">写入命令行的确认信息</param>
/// <param name="showWindow">是否显示窗口</param>
/// <returns></returns>
private static string ExecuteCMD(string cmd string arg string comfirm bool showWindow)
{
Process p = new Process();
p.StartInfo.FileName = cmd;
p.StartInfo.Arguments = arg;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// 是否显示窗口
p.StartInfo.CreateNoWindow = !showWindow;
// 窗口状态
if (showWindow)
{
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
}
else
{
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
p.Start();
if (comfirm != null)
p.StandardInput.WriteLine(comfirm);
string msg = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return msg;
}
/// <summary>
/// 在运行脚本之前把脚本中的数据库名称替换成安装界面输入的数据库名称
/// </summary>
/// <param name="filePath">脚本文件名</param>
/// <param name="oldDBName">原有的数据库名称</param>
/// <param name="newDBName">新的数据库名称</param>
public static void ReplaceDBName(string filePath string oldDBName string newDBName)
{
if (newDBName.CompareTo(oldDBName) != 0)
{
string fileText = string.Empty;
using (StreamReader streamReader = new StreamReader(filePath Encoding.Default))
{
fileText = streamReader.ReadToEnd();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论