本文转自:https://blog.csdn.net/tvmerp/article/details/1822669

下面是使用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帮助文档

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1769232

[转]使用C#调用cmd来执行sql脚本的更多相关文章

  1. .NET调用osql.exe执行sql脚本创建表和存储过程

    using System;using System.Diagnostics;using System.Windows.Forms; namespace WindowsFormsApplication1 ...

  2. 用cmd命令执行SQL脚本

    1.简单说明 osql 为SQL Server的命令 2.要在cmd中执行该命令,一般安装SQL Server后该命令对应的路径会自动添加到系统环境变量中. 3.-S 表示要连接的数据库 -U表示登录 ...

  3. 用C#中实现的,调用CMD来执行BCP的代码

    用C#中实现的,调用CMD来执行BCP的代码 用c#中实现,调用cmd来执行bcp的代码,大家共享!引用空间:using System;using System.Data;using System.D ...

  4. Inno Setup执行SQL脚本的方法

    作为和NSIS并立的.两个最流行的免费Windows应用程序安装包制作工具之一,Inno在学习难度上相对要低一些,非常适合对一些简单的桌面程序打包.但对于较复杂的安装过程,或者Web应用程序来说,我个 ...

  5. java 执行sql脚本的3种方式 (ant,ibatis,ScriptRunner)

    package com.unmi; import java.io.*; import org.apache.tools.ant.*; import org.apache.tools.ant.taskd ...

  6. mysql执行sql脚本

    最近用mysql执行sql脚本,遇到一些问题,顺便记录一下笔记. 首先,先开启mysql服务,创建一个空数据库(脚本里没有创建数据库) 执行脚本有两个方法 1.未连接数据库:在Windows下使用cm ...

  7. .net(C#)在Access数据库中执行sql脚本

    自己写的一个工具类,主要是业务场景的需要. 主要有两个功能: ①执行包含sql语句的字符串 ②执行包含sql语句的文件 调用方式 /// <summary> /// 执行sql语句 /// ...

  8. mysql执行sql脚本文件

    mysql执行sql脚本文件 方法一:使用cmd命令执行(windows下,unix或Linux在的其控制台下) [MySQL的bin目录]\mysql –u用户名 –p密码 –D数据库<[sq ...

  9. 使用命令执行 sql 脚本文件

    使用命令执行 sql 脚本文件 方法: 在 Windows 下使用 cmd 命令执行(或 Unix 或 Linux 控制台下)[Mysql的bin目录]\mysql –u用户名 –p密码 –D数据库名 ...

随机推荐

  1. R12 AR INVOICE 接口表导入

    http://blog.csdn.net/fangz0615/article/details/38677085 Purpose 本文介绍了如何通过AR接口表进行AR事务处理(亦称AR发票)导入. Ap ...

  2. DevExpress的TcxDBLookupComboBox使用方法及问题

    使用TcxDBLookupComboBox需要设置以下属性: 1.DataBinding.DataSource:数据感知控件的数据源. 2.DataBinding.DataField:数据感知控件的连 ...

  3. Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...

  4. C#把汉字转换成16进制(HEX)并向串口发送数据

    报警器实例:(有发送,无返回获取) using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  5. C#知识点提炼期末复习专用

    根据内部消息称:有三类题型:  程序阅读题:2题  简答题:2题 (主要是对概念的考查)  编程题:暂定2-3题 复习要点: .net framework 通用语言开发环境..NET基础类库..NET ...

  6. nyoj 1274信道安全 第九届河南省赛(SPFA)

    信道安全 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Alpha 机构有自己的一套网络系统进行信息传送.情报员 A 位于节点 1,他准备将一份情报 发送给位于节点 ...

  7. WebDriver高级应用实例(5)

    5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...

  8. 解决 ORA-27102: out of memory

    记一次故障处理总结: 操作系统:windows server 2008 R2数据库版本:11.2.0.2 故障描述:外部应用连接数据库,提示连接不正常: 排错过程:1.首先 检查数据库监听服务状态是否 ...

  9. sql练习(针对Mysql)

    创建表: DROP TABLE DEPT; --部门表 CREATE TABLE DEPT( DEPTNO int PRIMARY KEY, DNAME ) , --部门名称 LOC ) ---部门地 ...

  10. IdentityServer-Protecting an API using Client Credentials

    使用客户凭证保护API 这篇快速开始将展示使用IdentityServer保护APIs的最基本使用场景. 在此场景中我们将定义一个API和一个要访问此API的客户端. 客户端将向IdentitySer ...