zoukankan      html  css  js  c++  java
  • C#中执行批处理文件(.bat),执行数据库相关操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    using System.Diagnostics;
    
    namespace Sample2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process proc = null;
                try
                {
                    /*
                     *   autorun.bat文件内容
                         osql -S 127.0.0.1 -d DataSample -U sa -P 1234567890 -i C:	est.sql
                     */
                    string targetDir = string.Format(@"C:");
                    proc = new Process();
                    proc.StartInfo.WorkingDirectory = targetDir;
                    proc.StartInfo.FileName = "autorun.bat";
                    proc.StartInfo.Arguments = string.Format("");//this is argument
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
                    proc.Start();
                    proc.WaitForExit();
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
                }
    
                /*  test.sql内容
                    USE [DataSample]
                    GO
    
                    SET ANSI_NULLS ON
                    GO
    
                    SET QUOTED_IDENTIFIER ON
                    GO
    
                    CREATE TABLE [dbo].[Area](
                        [AreaId] [int] IDENTITY(1,1) NOT NULL,
                        [AreaName] [nvarchar](50) NULL,
                        [CityId] [int] NULL,
                     CONSTRAINT [PK_Area] PRIMARY KEY CLUSTERED 
                    (
                        [AreaId] ASC
                    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
                    ) ON [PRIMARY]
    
                    GO             
                 */
            }
        }
    }
  • 相关阅读:
    端口映射到公网工具
    C# app.config文件配置和修改
    C#基本知识点-Readonly和Const的区别
    C#知识点-StopWatch-计时
    python with open as f写中文乱码
    程序员不要去这样的公司
    关于老系统的重构和优化选择
    .Net Remoting 调用远程对象
    自定义的Config节点及使用
    前台线程和后台线程区别与使用技巧
  • 原文地址:https://www.cnblogs.com/smartsmile/p/7667036.html
Copyright © 2011-2022 走看看