システムマネージャでデータベースをバックアップし、データベースを復元する方法[コード]
11041 ワード
namespace BLL
{
public class SystemManager
{
private DAL.SystemService service = new DAL.SystemService();
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string DataSoureBack(string path,string oName)
{
string demo = service.DataSoureBack(path);
MODEL.BackMsg model= new MODEL.BackMsg {
BackPath=path,
Operated=oName,
};
if (string.IsNullOrEmpty(demo) || demo == "success")
{
model.OperatedResult = " ";
model.ReasonsFailure = "";
}
else
{
model.OperatedResult = " ";
model.ReasonsFailure = demo;
}
new BLL.BackMsgManager().AddBackMsg(model);
return demo;
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string RestoreData(string path)
{
return service.RestoreData(path);
}
}
}
namespace DAL
{
public class SystemService
{
public string DataSoureBack(string path)
{
string result = string.Empty;
SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=Dhcs)_+;");
SqlCommand cmdBK = new SqlCommand();
cmdBK.CommandType = CommandType.Text;
cmdBK.Connection = conn;
cmdBK.CommandText = @"backup database lnsmB2B to disk='" + path + "' with init";
try
{
conn.Open();
cmdBK.ExecuteNonQuery();
result = "success";
}
catch (Exception ex)
{
result = ex.Message;
}
finally
{
conn.Close();
conn.Dispose();
}
return result;
}
public string RestoreData(string path)
{
string temp = string.Empty;
SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=Dhcs)_+;Trusted_Connection=False");
conn.Open();
//KILL DataBase Process
SqlCommand cmd = new SqlCommand("SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='lnsmB2B'", conn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
ArrayList list = new ArrayList();
while (dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
for (int i = 0; i < list.Count; i++)
{
cmd = new SqlCommand(string.Format("KILL {0}", list[i]), conn);
cmd.ExecuteNonQuery();
}
SqlCommand cmdRT = new SqlCommand();
cmdRT.CommandType = CommandType.Text;
cmdRT.Connection = conn;
cmdRT.CommandText = @"restore database lnsmB2B from disk='" + path + "'";
try
{
cmdRT.ExecuteNonQuery();
temp = "success";
}
catch (Exception ex)
{
temp = ex.Message;
}
finally
{
conn.Close();
}
return temp;
}
}
}
namespace BLL
{
public class BackMsgManager
{
private DAL.BackMsgService service = new DAL.BackMsgService();
/// <summary>
///
/// </summary>
/// <returns></returns>
public IList<MODEL.BackMsg> GetBackMsg()
{
return service.GetBackMsg();
}
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool AddBackMsg(MODEL.BackMsg model)
{
return service.AddBackMsg(model);
}
/// <summary>
/// ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool DeleteBackMsg(string id)
{
return service.DeleteBackMsg(id);
}
}
}
////
そしてweb.configで保存するパスを設定する