C#中执行批处理文件(.bat),执行数据库相关操作_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#中执行批处理文件(.bat),执行数据库相关操作

C#中执行批处理文件(.bat),执行数据库相关操作

 2017/10/14 15:25:47  深南大道  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Configuration;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Diagnostics;namespaceSample2{classProgram{staticvoidMain
  • 标签:C# 文件 数据库 数据 执行 操作
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             
             */
        }
    }
}

 

发表评论
用户名: 匿名