利用.net(C#)执行sql脚本的简单实现
2008-04-02 08:35
585 查看
下面是使用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语句特别注意大小写! 可用-U username 或/U username,但不能用-u username
对于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语句特别注意大小写! 可用-U username 或/U username,但不能用-u username
对于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(C#)执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- 在.net执行sql脚本的简单实现
- ASP.NET(C#)执行.SQL脚本实现数据库建表
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库
- .net利用Timer和Global.asax实现定时执行程序C#
- 利用 Ant 的 SQL Task 来实现自己的 Java 执行 SQL 脚本文件的功能
- C#利用反射实现(js)Eval,asp(Execute)功能 动态执行类库中的函数
- 利用Roslyn构建一个简单的C#交互脚本引擎
- C#执行SQL脚本文件
- C#动态执行函数:利用反射实现
- 一种利用线程池线程执行任务, 并能够结束超时任务的方法 (.NET实现)
- [C#]利用 DynamicLinq 实现简单的动态表达式构建查询
- 一个简单的利用ini配置文件以及用户输入的交互进行文件夹内容拷贝的批处理文件脚本的实现
- C#中如何执行sql脚本?