在.net执行sql脚本的简单实现
来源:互联网 发布:香港恒生指数年度数据 编辑:程序博客网 时间:2024/04/29 21:19
在.net执行sql脚本的简单实现
郑佐2004-12-25
看到csdn社区经常有人问在.net中如果执行sql脚本,下面是使用C#调用cmd来执行osql实现脚本的执行。
using System;
using System.Data;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace ZZ
{
public class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string sqlQuery = "osql.exe /uSa /p123 /s192.192.132.229 /dNorthWind /i yoursql.sql";
string strRst = ExeCommand(sqlQuery);
Console.WriteLine(strRst);
Console.ReadLine();
}
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
}
}
对于osql命名的参数如下:
=====================
用法: osql [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w columnwidth]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-L list servers] [-c cmdend] [-D ODBC DSN name]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr] [-V severitylevel]
[-i inputfile] [-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
[-? show syntax summary]
具体参考
http://www.588188.com/netbook/sqlserver2000/coprompt/cp_osql_1wxl.htm
或者sql server 2000帮助文档
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 利用.net(C#)执行sql脚本的简单实现
- 利用.net(C#)执行sql脚本的简单实现
- .net中sql脚本的执行
- ASP.NET(C#)执行.SQL脚本实现数据库建表
- .net执行SQL脚本文件
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库
- [原创]完整的在.net后台执行javascript脚本集合
- (三)关于SQL脚本在DbCommand里的执行
- 在mysql中执行创建好的sql脚本
- 在android中批量执行sqlite的sql脚本
- 如何知道脚本中正在执行的SQL语句
- 实现SQL脚本执行结果的界面输出
- bat脚本实现sqlserver的sql语句执行
- J2SDK1.4新增的Java日志框架
- SQL查询重复记录
- Spring bean的定义
- ServiceMix(一) 基础知识和安装
- vim + cscope
- 在.net执行sql脚本的简单实现
- ServiceMix(二) 控制台简介
- ServiceMix(三) 部署简单的项目
- ServiceMix(四) bundles之间的简单沟通
- 西安停车场将设立城市一卡通结算平台
- ServiceMix(五) bundles in ServiceMix
- 衡水年内计划发放万张农民工“一卡通” 防止拖欠工资
- 金融社保IC卡将逐步拓展到民政计生等领域
- 发力城市一卡通,创建物联网信息化城市