#include "StdAfx.h"
#include "OperatorData.h"
extern CString szVariable;
//////////////////////////////////// 类的成员函数 ////////////////////
//默认构造函数
CDatabase::CDatabase()
{
//设置数据源
m_DatabaseScore="";
//数据库备份转换
m_str = "";
//数据库备份路径
m_strPath = "";
//数据库的用户名
m_userName = "";
//数据库的密码
m_passWord = "";
}
//默认析构函数
CDatabase::~CDatabase()
{}
// 数据库接口
bool CDatabase::interFace(CString szPath)
{
/* cout<<"请输入数据库的用户名:";
char name[20];
cin>>name;
// strcpy(m_userName,name);
cout<<"请输入数据库的密码:";
char password[20];
cin>>password;*/
// strcpy(m_passWord,password);
//数据库的用户名
// szUserName = "";
//数据库的密码
// szPassWord = "";
//////////////////////////////////////////////////////
//数据库的用户名
m_userName = "sa";//"sa";
//数据库的密码
m_passWord = "";//"sa";
//数据库备份,恢复路径
m_strPath = szPath;
//////////////////////////////////////////////////////////////////////////////
return true;
}
// 配置函数
bool CDatabase::configurationInfomation()
{
CoInitialize(NULL);
// 设置数据库连接信息
int hr =-1;
try
{
//1 创建Connection对象
hr = m_pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
CString szSQL;
CString name = CString("sa");//数据库的用户名
CString pass = "sa"; //数据库的密码
CString Source="MICROSOF-120A44"/*szVariable*/; //数据库的服务器名称
//初始化数据源
szSQL.Format("Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=master;Data Source=%s",pass,name,Source);
//连接数据库
m_pConnection->Open((_bstr_t)szSQL,"","",adModeUnknown);
}
else
{
AfxMessageBox("初始化 Connectiong 失败");
}
//2 创建Recordptr对象
hr= m_pRecordSet.CreateInstance("ADODB.Recordset");
if(SUCCEEDED(hr)) ;else AfxMessageBox("初始化 ADODB.Recordset 失败");//cout<<"初始化 ADODB.Recordset 失败"<<endl;
//3 创建Command对象
hr =m_pCommand.CreateInstance("ADODB.Command");
if(SUCCEEDED(hr)) ;else AfxMessageBox("初始化 ADODB.Command 失败");//cout<<"初始化 ADODB.Command 失败"<<endl;
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!错误信息:%s",e.ErrorMessage());
//errormessage.Format("连接数据库失败!错误信息:%s",e);
AfxMessageBox(errormessage);
return FALSE;
}
//信息配置成功
return true;
}
// 设置数据库备份
bool CDatabase::backupDatabase()
{
try
{
m_pCommand->ActiveConnection = m_pConnection;//建立连接
m_str.Format("backup database HiView to disk = %s with init",m_strPath);//语句赋值
m_pCommand->CommandText=_bstr_t(m_str);//
m_pRecordSet = m_pCommand->Execute(NULL,NULL,adCmdText);//执行语句
}
//捕获异常
catch(_com_error e)
{
CString errormessage;
errormessage.Format("备份数据库失败!错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return false;
}
// this->MessageBox("恭喜您,数据库备份成功!");
return true;
}
// 恢复数据库备份
bool CDatabase::restoreDatabase()
{
try
{
m_pCommand->ActiveConnection = m_pConnection;
m_str.Format("use master restore database HiView from disk = %s with replace",m_strPath);//with recovery,replace
m_pCommand->CommandText=_bstr_t(m_str);
//执行数据库恢复
m_pRecordSet = m_pCommand->Execute(NULL,NULL,adCmdText);
//断开数据库
m_str.Format("USE master ALTER DATABASE HiView SET OFFLINE WITH ROLLBACK IMMEDIATE");//with recovery,replace
m_pCommand->CommandText=_bstr_t(m_str);
m_pRecordSet = m_pCommand->Execute(NULL,NULL,adCmdText);
//重新连接数据库
m_str.Format("USE master ALTER DATABASE HiView SET ONLINE WITH ROLLBACK IMMEDIATE");//with recovery,replace
m_pCommand->CommandText=_bstr_t(m_str);
m_pRecordSet = m_pCommand->Execute(NULL,NULL,adCmdText);
}
//捕获异常
catch(_com_error e)
{
CString errormessage;
errormessage.Format("数据库恢复失败!错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return false ;
}
//this->MessageBox("恭喜您,数据库还原成功!");
return true;
}
// 清空表
bool CDatabase::emptyTable(CString cstableName)
{
try
{
m_DataBase_Name = "HiView";
m_strPath = "'d:\\abc.bak'";
m_pCommand->ActiveConnection = m_pConnection;
CString m_str;
m_str.Format("use %s truncate table %s",m_DataBase_Name,cstableName);
m_pCommand->CommandText=_bstr_t(m_str);
m_pRecordSet = m_pCommand->Execute(NULL,NULL,adCmdText);
}
//捕获异常
catch(_com_error e)
{
CString errormessage;
errormessage.Format("清空表失败!/r/n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return false;
}
//this->MessageBox("恭喜您,表清理成功!");
return true;
}