C#日记6-- 制作安装包中添加数据库

  1.  
     public override void Install(System.Collections.IDictionary stateSaver)
  2.         {
  3.             base.Install(stateSaver);
  4.             try
  5.             {
  6.                 string targetdir = this.Context.Parameters["targetdir"].ToString();
  7.                 string filePath = "D://dbpath";
  8.                 //文件拷贝
  9.                 if (!System.IO.Directory.Exists(filePath))
  10.                 {
  11.                     System.IO.Directory.CreateDirectory(filePath);
  12.                 }
  13.                 String[] Files = System.IO.Directory.GetFiles(targetdir + "//dbpath");
  14.                 foreach (string file in Files)
  15.                 {
  16.                     File.Copy(file, filePath + "//" + file.Substring(file.LastIndexOf("//") + 1, file.Length - file.LastIndexOf("//") - 1), true);
  17.                 }                
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 Logger.Debug(ex.Message);                
  22.             }
  23.             try
  24.             {
  25.                 System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
  26.                 sqlProcess.StartInfo.FileName = "osql.exe";
  27.                 sqlProcess.StartInfo.Arguments = string.Format(@" -S {0} -U {1} -P {2} -d {3} -i {4}AttachDB.sql"this.Context.Parameters["server"], this.Context.Parameters["user"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"], this.Context.Parameters["targetdir"]);
  28.                 Logger.Debug(sqlProcess.StartInfo.Arguments);
  29.                 sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  30.                 sqlProcess.Start();
  31.                 sqlProcess.WaitForExit(); //等待执行
  32.             }
  33.             catch (Exception ex)
  34.             {
  35.                 Logger.Debug(ex.Message);                
  36.             }
  37.         }       

AttachDB.sql文件

  1. EXEC sp_attach_db @dbname=N'mydb',
  2.     @filename1=N'D:/dbpath/mydb.mdf',
  3.     @filename2=N'D:/dbpath/mydb_log.ldf' 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值