CEL_Payroll/Payroll.BO/Query Analyzer/UtilsDA.cs
2024-09-17 14:30:13 +06:00

75 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Payroll.BO
{
class UtilsDA
{
public UtilsDA() { }
public static void BackupDatabase(ETransactionContext tc, string dbName, string filepath)
{
tc.ExecuteNonQuery("BACKUP DATABASE %q TO DISK=%s", dbName,filepath);
}
public static void RunCMD(ETransactionContext tc, string sql)
{
tc.ExecuteNonQuery(sql);
}
public static void RestorDatabase(ETransactionContext tc, string dbName, string filepath)
{
tc.ExecuteNonQuery("RESTORE DATABASE %q FROM DISK=%s WITH REPLACE,RECOVERY", dbName, filepath);
}
public static void RestorDatabase(ETransactionContext tc, string sDBName, string sBackupFilePath, string sToDataFilePath, string sToLogFilePath)
{
string sDataFile = sDBName + "_" + "Data.mdf";
string sLogFile = sDBName + "_" + "Log.ldf";
DataSet oDSFilelist = tc.ExecuteDataSet("RESTORE FILELISTONLY FROM DISK = %s", sBackupFilePath);
string sBKDataFile = "";
string sBKLogFile = "";
if (oDSFilelist != null && oDSFilelist.Tables.Count > 0)
{
if (oDSFilelist.Tables[0].Rows != null && oDSFilelist.Tables[0].Rows.Count > 0)
{
sBKDataFile = oDSFilelist.Tables[0].Rows[0][0].ToString();
sBKLogFile = oDSFilelist.Tables[0].Rows[1][0].ToString();
}
}
//string sSQL =SQLParser.MakeSQL("RESTORE DATABASE %q"
// + " FROM DISK = %s "
// + " WITH MOVE %s TO %s " + @"%s" + ", "
// + " MOVE %s TO %s" + @" %s", sDBName, sBackupFilePath, sBKDataFile, sToDataFilePath, sDataFile, sBKLogFile, sToLogFilePath, sLogFile);
string sSQL = "RESTORE DATABASE " + sDBName
+ " FROM DISK = '" + sBackupFilePath + "' "
+ " WITH MOVE '" + sBKDataFile + "' TO '" + sToDataFilePath + @"\" + sDataFile + "', "
+ " MOVE '" + sBKLogFile + "' TO '" + sToLogFilePath + @"\" + sLogFile + "' ";
tc.ExecuteNonQuery(sSQL);
}
public static bool IsExist(ETransactionContext tc, string dbName)
{
object obj= tc.ExecuteScalar("SELECT name FROM master.dbo.sysdatabases WHERE name = N%s", dbName);
if (obj != DBNull.Value && obj != null) return true;
return false;
}
public static IDataReader GetDatabases(ETransactionContext tc)
{
return tc.ExecuteReader("sp_databases");
}
}
}