请教如何用C#代码创建SQL数据库视图

2024-11-29 06:21:49
推荐回答(1个)
回答1:

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 调用命令行工具

///


/// 调用命令行工具
///

/// 命令行工具名称
/// 可选命令行参数
/// 注意:所有命令行工具都必须保存于system32文件夹中
///
private string CommandLine(string name, params string[] args)
{
return CommandLine(name, "", args);
}

///
/// 调用命令行工具
///

/// 命令行工具名称
/// 设置工作目录
/// 可选命令行参数
/// 注意:所有命令行工具都必须保存于system32文件夹中
///
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
}
}