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
*/
}
}
}C#中执行批处理文件(.bat),执行数据库相关操作
最新推荐文章于 2021-11-24 16:52:26 发布
此C#程序展示了如何通过执行批处理文件(autorun.bat)来运行SQL数据库操作。批处理文件包含osql命令,用于连接到127.0.0.1上的DataSample数据库,并导入C: est.sql文件中的SQL脚本。test.sql文件内容包括创建一个名为Area的表,带有主键约束。

799

被折叠的 条评论
为什么被折叠?



