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:\test.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             
             */
        }
    }
}

 

posted @ 2017-10-14 13:45  深南大道  阅读(1654)  评论(0编辑  收藏  举报