Files
HTCloud/HT.Cloud.Data/Extensions/DbHelper.cs

56 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*******************************************************************************
* Copyright © 2020 HT.Cloud.Framework 版权所有
* Author: HT.Cloud
* Description: WaterCloud快速开发平台
* Website
*********************************************************************************/
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using HT.Cloud.Code;
namespace HT.Cloud.DataBase.Extensions
{
public class DbHelper
{
private static string connstring = AESKeyEncrypt.DecryptDES(GlobalContext.SystemConfig.DBConnectionString);
private static string dbType = GlobalContext.SystemConfig.DBProvider;
public static int ExecuteSqlCommand(string database, string backupPath)
{
try
{
string backupFile = string.Format("{0}\\{1}_{2}.bak", backupPath, database, DateTime.Now.ToString("yyyyMMddHHmmss"));
using (DbConnection conn = new SqlConnection(connstring))
{
string strSql = string.Format(" backup database [{0}] to disk = '{1}'", database, backupFile);
DbCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, null, CommandType.Text, strSql, null);
return cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
return 0;
}
}
private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (isOpenTrans != null)
cmd.Transaction = isOpenTrans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
cmd.Parameters.AddRange(cmdParms);
}
}
}
}