查看: 893|回复: 0

[ASP.NET教程] C#中执行批处理文件(.bat),执行数据库相关操作

发表于 2017-10-22 07:36:43
太阳http代理AD
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Configuration;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using System.Diagnostics;
  10. namespace Sample2
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. Process proc = null;
  17. try
  18. {
  19. /*
  20. * autorun.bat文件内容
  21. osql -S 127.0.0.1 -d DataSample -U sa -P 1234567890 -i C:\test.sql
  22. */
  23. string targetDir = string.Format(@"C:\");
  24. proc = new Process();
  25. proc.StartInfo.WorkingDirectory = targetDir;
  26. proc.StartInfo.FileName = "autorun.bat";
  27. proc.StartInfo.Arguments = string.Format("");//this is argument
  28. proc.StartInfo.CreateNoWindow = true;
  29. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
  30. proc.Start();
  31. proc.WaitForExit();
  32. Console.ReadKey();
  33. }
  34. catch (Exception ex)
  35. {
  36. Console.WriteLine("Exception Occurred :{0},小贝", ex.Message, ex.StackTrace.ToString());
  37. }
  38. /* test.sql内容
  39. USE [DataSample]
  40. GO
  41. SET ANSI_NULLS ON
  42. GO
  43. SET QUOTED_IDENTIFIER ON
  44. GO
  45. CREATE TABLE [dbo].[Area](
  46. [AreaId] [int] IDENTITY(1,1) NOT NULL,
  47. [AreaName] [nvarchar](50) NULL,
  48. [CityId] [int] NULL,
  49. CONSTRAINT [PK_Area] PRIMARY KEY CLUSTERED
  50. (
  51. [AreaId] ASC
  52. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  53. ) ON [PRIMARY]
  54. GO
  55. */
  56. }
  57. }
  58. }
复制代码



太阳http代理AD
回复

使用道具 举报