使用C#调用cmd来执行sql脚本
来源:互联网 发布:mac版xampp使用教程 编辑:程序博客网 时间:2024/06/04 19:28
下面是使用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帮助文档
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帮助文档
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1769232
- 使用C#调用cmd来执行sql脚本
- java通过cmd命令调用sqlplus执行sql脚本
- C# 调用并执行SQL脚本文件
- C# 调用并执行SQL脚本文件
- cmd 执行oracle sql脚本
- C# 调用执行CMD命令
- c# 调用CMD命令行,执行
- c#调用CMD执行程序
- 调用方法执行sql脚本来创建数据库
- cmd 执行sql脚本 sql server
- C#调用SQLCMD去批量执行sql脚本
- C# 调用并执行SQL脚本文件
- 在CMD中执行sql脚本
- 用c#中实现的,调用cmd来执行bcp的代码
- C#如何调用cmd程序的执行
- c#中调用cmd命令执行批处理文件
- c#中调用cmd命令执行批处理文件
- c# 调用CMD命令执行dos命令
- MATLAB bulider for JAVA 使用TOMCAT 进行远程部署
- 最完美的c程序
- 一个有用的Windows服务小程序——用来完成Server端的Socket通信
- Visual Studio 2005 无法显示设计视图的解决方法
- Scott Mitchell 的ASP.NET 2.0数据教程之一: 创建一个数据访问层
- 使用C#调用cmd来执行sql脚本
- 爱一个人真的很难吗
- Scott Mitchell 的ASP.NET 2.0数据教程之二:创建一个业务逻辑层
- 在Asp.net 2.0中操作数据::母板页和站点导航
- 什么是CDN
- 实现编号查询的另类思路
- 什么是DHCP?
- Tell the Java VM to Load the DLL
- 强制性qq聊天