//public void btnrestore(string name) //{ // string tmp = AdName.Text; // tmp = name; // SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString()); // string dbFileName = ""; // dbFileName = tmp; // SqlCommand command = new SqlCommand("use master ;ALTER DATABASE [BudgetNetDB] SET OFFLINE WITH ROLLBACK IMMEDIATE;restore database @name from disk=@path WITH REPLACE;", connection); // connection.Open(); // string path = ""; // path = Server.MapPath("~\\DataFile"); // path = path + "\\" + @dbFileName; // command.Parameters.AddWithValue("@name", "BudgetNetDB"); // command.Parameters.AddWithValue("@path", path); // command.ExecuteNonQuery(); // connection.Close(); // X.Msg.Alert("系统提示", "还原成功!").Show(); //} public void btnrestore(string dbid) { string dbname = DBUnity.connectionString.Split(';')[1].Split('=')[1]; string tmp = BG_Unit_DbbakService.GetBG_Unit_DbbakByDbID(Common.common.IntSafeConvert(dbid)).DbName; string connectionStringserver = ConfigurationManager.ConnectionStrings["ConnectionStringserver"].ToString(); string sql = " DECLARE @sql NVARCHAR (MAX)" + " DECLARE @sql1 NVARCHAR (MAX)" + " DECLARE @sql2 NVARCHAR (MAX)" + " DECLARE @i int" + " DECLARE @tabname NVARCHAR (MAX)" + " set @i=1" + " while @i<=(select count(tab.name) from {1}.dbo.SysObjects as tab where xtype='u' and name not like '%_Unit_%' )" + " begin" + " set @tabname=(select top(1) * from (select top(@i) tab.name from {1}.dbo.SysObjects as tab where xtype='u' and name not like" + " '%_Unit_%' order by name asc) as a order by name desc)" + " SET @sql1 = 'select * into {1}.dbo.'+@tabname+' from ' +'{0}.[dbo].'+@tabname " + " SET @sql2 = 'drop table {1}.[dbo].'+@tabname +' ' + @sql1 " + " SET @sql = ''if object_id(N'{1}.[dbo].'+@tabname+'',N'U') is not null EXEC(@sql2) else EXEC(@sql1) " + " EXEC(@sql); " + " set @i= @i+1 " + " end "; sql = string.Format(sql, tmp, dbname); int t = DBUnity.ExecuteNonQuery(CommandType.Text, sql, null); if (t > 0) { FileStream aFile = new FileStream(Server.MapPath("~\\DataFile") + "\\" + "存储过程.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(aFile, Encoding.UTF8); string sqlccgc = sr.ReadToEnd(); sr.Close(); aFile.Close(); int c = 0; try { string serversql = string.Format("EXEC sp_dropserver 'JZDBServer' , 'droplogins' EXEC sp_addlinkedserver 'JZDBServer','','SQLNCLI','{0}' EXEC sp_addlinkedsrvlogin 'JZDBServer','false',NULL,'sa','{1}'", connectionStringserver.Split(';')[0].Split('=')[1], connectionStringserver.Split(';')[3].Split('=')[1]); DBUnity.ExecuteNonQuery(CommandType.Text, serversql, null); string[] arr = System.Text.RegularExpressions.Regex.Split(sqlccgc.Replace("\r\n"," "), "GO"); try { for (int n = 0; n < arr.Length; n++) { string strsql = arr[n]; if (strsql.Trim().Length > 1) { DBUnity.ExecuteNonQuery(CommandType.Text, strsql, null); } } c++; } catch { c = 0; } } catch (Exception ex) { if (ex.ToString().Contains("已存在")) { c++; } } if (c > 0) { X.Msg.Alert("系统提示", "还原成功!").Show(); } else { X.Msg.Alert("系统提示", "还原失败!").Show(); } } }
public static BG_Unit_Dbbak GetBG_Unit_DbbakByDbID(int dbID) { return(BG_Unit_DbbakService.GetBG_Unit_DbbakByDbID(dbID)); }