本文转自: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. 点聚weboffice插件自定义菜单

    https://blog.csdn.net/u014547764/article/details/53818637 1.在jsp中引用excel插件:首先项目中要有:WebOffice.zip.web ...

  2. 设置UITableView分割线距左边的间距

    ``` [self.tableView setSeparatorInset:UIEdgeInsetsZero]; [self.tableView setLayoutMargins:UIEdgeInse ...

  3. [leetcode 14]Longest Common Prfix

    1 题目: Write a function to find the longest common prefix string amongst an array of strings. Hide Ta ...

  4. 用WPF写一个登录界面,我想在输入完密码后按回车就能够验证登陆,而不需要用鼠标单击登陆按钮

    在wpf中,将按钮的IsDefault设置为true ​​​​

  5. NHibernate问题求大神解决!!!

    这是我定义的实体类 对应的数据库表 映射文件 数据访问层写的是插入语句 错误: 捕捉到 NHibernate.Exceptions.GenericADOException HResult=-21462 ...

  6. win7 docker 挂载共享目录

    在 win7 下用 docker 不像 win10 那样方便,安装包都不一样. 在 win7 下共享一个目录的方法如下: 1. 先设置 win7 到 VirtualBox 中 docker 用的那个虚 ...

  7. [Element-UI] 使用Element-UI的DateTimePicker组件报错:Cannot read property 'getHours' of undefined

    使用Element-UI组件的DateTimePicker,如下: <template> <div class="block"> <span clas ...

  8. JS简单表单验证

    这里我是写了一个简单的注册表单验证功能,亲测有效,一起来看看吧! 首先我的HTML代码是这样的: class大家可以忽略一下,这里我项目使用的是bootstrap的样式. 输入用户名和密码用的是正则表 ...

  9. POJ 2485

    #include<iostream> #define MAXN 505 #define inf 1000000000 using namespace std; typedef int el ...

  10. 机器人之路的第一小步:录音+语音识别(语音转文字),大小600K(免费下载)!

    机器人之路的第一小步:录音+语音识别(语音转文字),大小600K,本人出品! 机器人之路的第一小步:录音+语音识别,准确率还不是特别高,不过普通话标准的话,识别准确率还是不错的,大家可以体验一下,请下 ...