.NET调用osql.exe执行sql脚本创建表和存储过程
文章出处:http://wenjl520.cnblogs.com/ 或 http://www.cnblogs.com/
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//参数
string[] args = new string[5];
args[0] = "-U " + txtUserName.Text; //用户名
args[1] = "-P " + txtPassword.Text; //用户密码
args[2] = "-S " + txtServer.Text; //服务器
args[3] = "-d " + txtDatabase.Text; //数据库
args[4] = "-i " + AppDomain.CurrentDomain.BaseDirectory + "Install.sql"; //sql脚本路径
//执行
textBox1.Text = CommandLine("osql.exe", args);
}
#region 调用命令行工具
/// <summary>
/// 调用命令行工具
/// </summary>
/// <param name="name">命令行工具名称</param>
/// <param name="args">可选命令行参数</param>
/// <remarks>注意:所有命令行工具都必须保存于system32文件夹中</remarks>
/// <returns></returns>
private string CommandLine(string name, params string[] args)
{
return CommandLine(name, "", args);
}
/// <summary>
/// 调用命令行工具
/// </summary>
/// <param name="name">命令行工具名称</param>
/// <param name="workingDirectory">设置工作目录</param>
/// <param name="args">可选命令行参数</param>
/// <remarks>注意:所有命令行工具都必须保存于system32文件夹中</remarks>
/// <returns></returns>
private string CommandLine(string name, string workingDirectory, params string[] args)
{
string returnValue = "";
using (Process commandline = new Process())
{
try
{
commandline.StartInfo.UseShellExecute = false;
commandline.StartInfo.CreateNoWindow = true;
commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
commandline.StartInfo.RedirectStandardOutput = true;
commandline.StartInfo.FileName = name;
commandline.StartInfo.WorkingDirectory = workingDirectory;
//添加命令行参数
if (args.Length > 0) commandline.StartInfo.Arguments = string.Join(" ", args);
commandline.Start();
commandline.WaitForExit();
returnValue = commandline.StandardOutput.ReadToEnd();
commandline.Close();
}
catch
{
commandline.Dispose();
throw;
}
}
return returnValue;
}
#endregion
}
}
复制代码
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//参数
string[] args = new string[5];
args[0] = "-U " + txtUserName.Text; //用户名
args[1] = "-P " + txtPassword.Text; //用户密码
args[2] = "-S " + txtServer.Text; //服务器
args[3] = "-d " + txtDatabase.Text; //数据库
args[4] = "-i " + AppDomain.CurrentDomain.BaseDirectory + "Install.sql"; //sql脚本路径
//执行
textBox1.Text = CommandLine("osql.exe", args);
}
#region 调用命令行工具
/// <summary>
/// 调用命令行工具
/// </summary>
/// <param name="name">命令行工具名称</param>
/// <param name="args">可选命令行参数</param>
/// <remarks>注意:所有命令行工具都必须保存于system32文件夹中</remarks>
/// <returns></returns>
private string CommandLine(string name, params string[] args)
{
return CommandLine(name, "", args);
}
/// <summary>
/// 调用命令行工具
/// </summary>
/// <param name="name">命令行工具名称</param>
/// <param name="workingDirectory">设置工作目录</param>
/// <param name="args">可选命令行参数</param>
/// <remarks>注意:所有命令行工具都必须保存于system32文件夹中</remarks>
/// <returns></returns>
private string CommandLine(string name, string workingDirectory, params string[] args)
{
string returnValue = "";
using (Process commandline = new Process())
{
try
{
commandline.StartInfo.UseShellExecute = false;
commandline.StartInfo.CreateNoWindow = true;
commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
commandline.StartInfo.RedirectStandardOutput = true;
commandline.StartInfo.FileName = name;
commandline.StartInfo.WorkingDirectory = workingDirectory;
//添加命令行参数
if (args.Length > 0) commandline.StartInfo.Arguments = string.Join(" ", args);
commandline.Start();
commandline.WaitForExit();
returnValue = commandline.StandardOutput.ReadToEnd();
commandline.Close();
}
catch
{
commandline.Dispose();
throw;
}
}
return returnValue;
}
#endregion
}
}
复制代码
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET