using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.IO;
namespace DBConection.service
{
public class DbService
{
//數據庫的備份
public bool BackUPDB(string strFileName, DB db)//DB是一個維護數據庫連接的連接池
{
using (System.Data.SqlClient.SqlConnection conn = db.GetConection())
{
System.Data.SqlClient.SqlCommand cmdBK = new SqlCommand();
cmdBK.CommandType = System.Data.CommandType.Text;
cmdBK.Connection = conn;
cmdBK.CommandText = @"backup database " + db.GetDBName() + " to disk='" + strFileName + "' with init";
try
{
conn.Open();
cmdBK.ExecuteNonQuery();
LogServcie.Log.writeLog("數據庫備份成功。/n備份地址:" + strFileName);
return true;
}
catch (Exception ex)
{
LogServcie.Log.WirteErr("數據庫備份失敗", ex);
}
finally
{
conn.Close();
conn.Dispose();
}
return false;
}
}
public void RestoreDB(string strDbName, string strFileName, string strToFileName, DB db)
{
using (System.Data.SqlClient.SqlConnection conn = db.GetConection())
{
DirectoryInfo dir = new DirectoryInfo(strToFileName);
if (!dir.Exists)
{
dir.Create();
}
string logicName = this.GetLogicName(strFileName, db);
if (strDbName.IndexOf(".bak") != -1)
{
strDbName = strDbName.Substring(0, strDbName.IndexOf(".bak"));
}
if (logicName != string.Empty)
{
conn.Open();
//還原數據庫
SqlCommand cmdRT = new SqlCommand();
cmdRT.CommandType = CommandType.Text;
cmdRT.Connection = conn;
cmdRT.CommandText = @"RESTORE DATABASE [" + strDbName + "] FROM DISK = N'" + strFileName + "' WITH FILE = 1, NOUNLOAD , STATS = 10, RECOVERY , MOVE N'" + logicName + "_Data' TO N'" + strToFileName + "//" + strDbName + ".mdf', MOVE N'" + logicName + "_Log' TO N'" + strToFileName + "//" + strDbName + "_log.ldf'";
try
{
cmdRT.ExecuteNonQuery();
LogServcie.Log.writeLog("數據庫還原成功。/n備份地址:" + strFileName + "/n 還原成數據庫:" + strDbName);
}
catch (Exception ex)
{
LogServcie.Log.WirteErr("數據庫還原失敗", ex);
}
finally
{
conn.Close();
}
}
}
}
/// <summary>
/// 用一個備份文件夾來恢復所有數據
/// </summary>
/// <param name="dir"></param>
/// <param name="strToFileName"></param>
/// <param name="logicName"></param>
/// <param name="db"></param>
public void RestoreDB(string[] Files, string strToFileName, DB db)
{
foreach (string fileName in Files)
{
FileInfo file = new FileInfo(fileName);
if (file.Exists)
RestoreDB(file.Name, file.FullName, strToFileName, db);
}
}
/// <summary>
/// 獲取數據庫的邏輯名
/// </summary>
/// <param name="bakPath"></param>
/// <param name="db"></param>
/// <returns></returns>
public string GetLogicName(string bakPath, DB db)
{
using (System.Data.SqlClient.SqlConnection conn = db.GetConection())
{
FileInfo file = new FileInfo(bakPath);
if (file.Exists)
{
conn.Open();
//還原數據庫
SqlCommand cmdRT = new SqlCommand();
cmdRT.CommandType = CommandType.Text;
cmdRT.Connection = conn;
cmdRT.CommandText = @"restore FILELISTONLY FROM DISK ='" + bakPath + "'";
try
{
object ob = cmdRT.ExecuteScalar();
string logicName = Convert.ToString(ob);
logicName = logicName.Substring(0, logicName.IndexOf("_"));
LogServcie.Log.writeLog("獲得數據庫邏輯名:" + logicName);
return logicName;
}
catch (Exception ex)
{
LogServcie.Log.WirteErr("數據庫還原失敗", ex);
}
finally
{
conn.Close();
}
}
return string.Empty;
}
}
}
}