日期:2014-05-18 浏览次数:20439 次
//备份 public bool DBBack(string path, string DBName) { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); string sql = "use master;"; sql += "backup database @dbName to disk=@path;"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@dbName", SqlDbType.Char); cmd.Parameters["@dbName"].Value = DBName; cmd.Parameters.Add("@path", SqlDbType.Char); cmd.Parameters["@path"].Value = DBName; try { conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } catch(Exception ex) { conn.Close(); return false; } return true; } //还原 public bool DBRestore(string path, string DBName) { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); string sql = "use master;"; sql += "restore database @dbName from disk=@path;"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@dbName", SqlDbType.Char); cmd.Parameters["@dbName"].Value = DBName; cmd.Parameters.Add("@path", SqlDbType.Char); cmd.Parameters["@path"].Value = DBName; try { conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { conn.Close(); return false; } return true; }