-
- {
- base.Install(stateSaver);
- try
- {
- string targetdir = this.Context.Parameters["targetdir"].ToString();
- string filePath = "D://dbpath";
- //文件拷贝
- if (!System.IO.Directory.Exists(filePath))
- {
- System.IO.Directory.CreateDirectory(filePath);
- }
- String[] Files = System.IO.Directory.GetFiles(targetdir + "//dbpath");
- foreach (string file in Files)
- {
- File.Copy(file, filePath + "//" + file.Substring(file.LastIndexOf("//") + 1, file.Length - file.LastIndexOf("//") - 1), true);
- }
- }
- catch (Exception ex)
- {
- Logger.Debug(ex.Message);
- }
- try
- {
- System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
- sqlProcess.StartInfo.FileName = "osql.exe";
- 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"]);
- Logger.Debug(sqlProcess.StartInfo.Arguments);
- sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- sqlProcess.Start();
- sqlProcess.WaitForExit(); //等待执行
- }
- catch (Exception ex)
- {
- Logger.Debug(ex.Message);
- }
- }
AttachDB.sql文件
- EXEC sp_attach_db @dbname=N'mydb',
- @filename1=N'D:/dbpath/mydb.mdf',
- @filename2=N'D:/dbpath/mydb_log.ldf'