本文转自: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. CRC校验3种算法_转载

    //CRC16校验在通讯中应用广泛,这里不对其理论进行讨论,只对常见的3种//实现方法进行测试.方法1选用了一种常见的查表方法,类似的还有512字//节.256字等查找表的,至于查找表的生成,这里也略 ...

  2. F - Cookies Piles

    Description The kids in my son's kindergarten made Christmas cookies with their teacher, and piled t ...

  3. FastReport报表设计(仔细看)

    FastReport报表设计 2011-06-16 16:56:19|  分类: 系统开发|举报|字号 订阅     下载LOFTER我的照片书  |     目录 5.1 前言 5.2 基本概念及操 ...

  4. lnmp mysql添加用户命令

    cd /usr/local/mysql/bin/grant all privileges on *.* to 'root'@'%' identified by '12345678';flush pri ...

  5. Python 高级编程——单例模式

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 在 Py ...

  6. 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom

    [源码下载] 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom 作者:webabcd 介绍 ...

  7. PowerDesigner执行脚本 name/comment/stereotype互转

    执行方法:工具栏->Tools -> Execute Commands -> Edit/Run Script (Ctrl+Shift+X) 如下图所示: 1.Name转到Commen ...

  8. leetcode 152. 乘积最大子序列 java

    题目: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. ...

  9. “全栈2019”Java多线程第三十六章:如何设置线程的等待截止时间

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 下一章 "全栈2019"J ...

  10. Linux巩固记录(6) Hbase环境准备-zookeeper安装

    Hbase是运行在hadoop之上,所以请参考第3篇文章搭建好一个master,两个slave的hadoop环境,我采用的版本为hadoop2.7.4 不了解Hbase的同学可以参考下这篇文章,分析得 ...