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             
             */
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM