首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > 在.net执行sql脚本的简单实现
【标  题】:在.net执行sql脚本的简单实现
【关键字】:.net,sql
【来  源】:http://www.cublog.cn/u/884/showart.php?id=113401

在.net执行sql脚本的简单实现

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帮助文档

《代码大全(第2版)》勘误表,2006 年 5 月 4 日更新,共 60 条:【上一篇】
全面剖析VB.NET:【下一篇】
【相关文章】
  • 在.NET中产生随机密码字符串
  • Asp.net(c#)实现多线程断点续传
  • ASP.NET画图全攻略
  • 在.Net中操作IIS
  • 用反射调用任意.Net库中的方法
  • 在ASP.NET中处理 datetime 的一些通用函数
  • MySQL 4.1 字符集支持的原理 (转)
  • MySQL的嵌套查询
  • 通过改善架构来提高 ASP.Net 应用程序的性能
  • ASP.NET 2.0中ReadOnly的TextBox
  • 【随机文章】
  • C# 编码规范
  • 类似qq上线提示的一个java程序
  • big endian vs little endian
  • SVG + Javascript + ASP.NET + WebService开发楼宇管理系统 - 杂记1
  • 数学工具
  • IIS服务器的特性
  • Java的Web框架对比
  • 黑帽06安全大会一改传统 聚焦Vista和IE7安全
  • 树结构查询
  • ODBC 数据库链接函数库
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.