不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
.NET调用osql.exe执行sql脚本创建表和存储过程
编辑:dnawo 日期:2009-11-26
复制内容到剪贴板 程序代码
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.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
}
}
若只是创建表,可以使用《.NET调用SQL脚本创建数据库及表示例》一文介绍的方法,不需要调用命令行就可以完成,比较简单,但该方法在创建存储过程时比较麻烦,老报错。使用本文的方法需要调用命令行工具osql.exe,可直接使用mssql生成的sql脚本,不需另外修改。
上一篇: Nokia 5610(S40V3)下载nth主题后自动改为ext格式导致主题无法使用解决方法
下一篇: Nokia5800XM使用WLAN访问本地网络
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
下一篇: Nokia5800XM使用WLAN访问本地网络
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 5149
发表评论
请登录后再发表评论!