⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbbackup.cpp

📁 自已做的简单的库存管理软件。包含员工信息管理等
💻 CPP
字号:
// DBBackup.cpp : 实现文件
//

#include "stdafx.h"
#include "DBBackup.h"

extern _ConnectionPtr pMyConnect;
extern CString str_database ;
extern CString str_database_server;
// CDBBackup 对话框

IMPLEMENT_DYNAMIC(CDBBackup, CDialog)

CDBBackup::CDBBackup(CWnd* pParent /*=NULL*/)
	: CDialog(CDBBackup::IDD, pParent)
{

}

CDBBackup::~CDBBackup()
{
}

void CDBBackup::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDBBackup, CDialog)
	ON_BN_CLICKED(IDOK, &CDBBackup::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_CHOOSE, &CDBBackup::OnBnClickedButtonChoose)
	ON_BN_CLICKED(IDC_BUTTON_RESTORE, &CDBBackup::OnBnClickedButtonRestore)
END_MESSAGE_MAP()


// CDBBackup 消息处理程序

void CDBBackup::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码

	if ( str_database_server != _T(".") )
	{
		AfxMessageBox(_T("备份将在服务器上进行,备份目录在D:"));
		
		_CommandPtr m_pCommand = NULL;//还是智能指针
		
		HRESULT hr = m_pCommand.CreateInstance(__uuidof(Command));
		m_pCommand->CommandText = "backup_time";
		m_pCommand->CommandType = adCmdStoredProc;
		
		
		m_pCommand->ActiveConnection = pMyConnect;//设置连接,别忘了啊
		try
		{
			m_pCommand->Execute(NULL,NULL,adCmdStoredProc); 
		}
		catch (_com_error &e)
		{
			::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
			//printf("%s\n", (LPCTSTR)e.Description());
			return;
		}
		AfxMessageBox(_T("备份成功!"));
	}
	else
	{
		
		CString str_path;
		CString str_filename;
		CWnd* pWnd;
		
		//BRAND
		pWnd = GetDlgItem(IDC_EDIT_PATH);
		pWnd->GetWindowText(str_path);
		if( str_path.GetLength() == 0 )
		{
			AfxMessageBox(_T("请选择备份路径!"));
			return;
		}
		pWnd = GetDlgItem(IDC_EDIT_FILENAME);
		pWnd->GetWindowText(str_filename);
		if( str_filename.GetLength() == 0 )
		{
			AfxMessageBox(_T("请输入备份文件名!"));
			return;
		}
		
		CString str_path_filename = str_path + _T("\\") + str_filename;
		CString m_strSql; 
		_RecordsetPtr m_pRecordset;
		_variant_t RecordsAffected;

		
		m_strSql.Format(_T("backup  database ") +str_database+ _T(" to disk = '") +str_path_filename+ _T("' with  init"));
		bool bSuccess = pMyConnect->Execute(_bstr_t(m_strSql),&RecordsAffected,adCmdText);
		
		if ( !bSuccess )
		{
			AfxMessageBox(_T("备份失败!"));
			return;
		}
		else
		{
			AfxMessageBox(_T("备份成功!"));
		}
	}
    
   /* _CommandPtr m_pCommand = NULL;//还是智能指针

	HRESULT hr = m_pCommand.CreateInstance(__uuidof(Command));
	m_pCommand->CommandText = "backup_time";
	m_pCommand->CommandType = adCmdStoredProc;

	_ParameterPtr m_pParam;
	m_pParam.CreateInstance(__uuidof(Connection));
	
	_ParameterPtr m_pParam1;
	m_pParam1.CreateInstance(__uuidof(Connection));
	
	_ParameterPtr m_pParam2;
	m_pParam2.CreateInstance(__uuidof(Connection)); 

	_ParameterPtr m_pParamRet;
	m_pParamRet.CreateInstance(__uuidof(Connection));

	m_pParam = m_pCommand->CreateParameter(_T("flag"),adVarChar,adParamInput,10,(_variant_t)_T("fl"));//给参数设置各属性
	m_pCommand->Parameters->Append(m_pParam);//加入到Command对象的参数集属性中
	
	m_pParam1 = m_pCommand->CreateParameter(_T("backup_db_name"),adVarChar,adParamInput,128,(_variant_t)_T("mytest"));
	m_pCommand->Parameters->Append(m_pParam1);
	
	m_pParam2 = m_pCommand->CreateParameter(_T("filename"),adVarChar,adParamInput,1000,(_variant_t)_T("d:\mytest.bak"));
	m_pCommand->Parameters->Append(m_pParam2);

    m_pCommand->ActiveConnection = pMyConnect;//设置连接,别忘了啊
	try
	{
		m_pCommand->Execute(NULL,NULL,adCmdStoredProc); 
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		//printf("%s\n", (LPCTSTR)e.Description());
	}*/

		OnOK();

}

void CDBBackup::OnBnClickedButtonChoose()
{
	// TODO: 在此添加控件通知处理程序代码
	CString sFolder=_T("");  
	LPMALLOC pMalloc;  
	
	//Gets the Shell's default allocator  
	if(::SHGetMalloc(&pMalloc) == NOERROR)  
	{  
		BROWSEINFO  bi;  
		TCHAR pszBuffer[MAX_PATH];  
		LPITEMIDLIST pidl;  
		
		bi.hwndOwner = GetSafeHwnd();  
		bi.pidlRoot = NULL;  
		bi.pszDisplayName = pszBuffer;  
		bi.lpszTitle = _T("选择一个文件夹...");  
		bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS; 
		
		bi.lpfn = NULL;  
		bi.lParam = 0;  
		
		//This next call issues the  dialog box.  
		if((pidl = ::SHBrowseForFolder(&bi)) !=  NULL)  
		{  
			if(::SHGetPathFromIDList(pidl, pszBuffer))  
			{    
				//At this point pszBuffer contains the selected path   
				sFolder = pszBuffer;  
			}
			//Free the PIDL allocated by SHBrowseForFolder.  
			pMalloc->Free(pidl);  
		}
		//Release the shell's allocator.  
		pMalloc->Release();
	}
	//return sFolder;

	CWnd* pWnd;
			
	//BRAND
	pWnd = GetDlgItem(IDC_EDIT_PATH);
	pWnd->SetWindowText(sFolder);
}

void CDBBackup::OnBnClickedButtonRestore()
{
	// TODO: 在此添加控件通知处理程序代码
		
	if ( str_database_server != _T(".") )
	{
		AfxMessageBox(_T("对不起,不支持网络备份数据库!"));
		return;
	}
		
	CString str_path;
	CString str_filename;
	CWnd* pWnd;
			
	//BRAND
	pWnd = GetDlgItem(IDC_EDIT_PATH);
	pWnd->GetWindowText(str_path);
	if( str_path.GetLength() == 0 )
	{
		AfxMessageBox(_T("请选择备份路径!"));
		return;
	}
	pWnd = GetDlgItem(IDC_EDIT_FILENAME);
	pWnd->GetWindowText(str_filename);
	if( str_filename.GetLength() == 0 )
	{
		AfxMessageBox(_T("请输入备份文件名!"));
		return;
	}

	CString str_path_filename = str_path + _T("\\") + str_filename;

	CStdioFile f2;
	if( !f2.Open( str_path_filename, CFile::modeRead ) ) 
	{
		AfxMessageBox(_T("无此文件!"));
		return;
	}

	CString m_strSql; 
	_RecordsetPtr m_pRecordset;
	_variant_t RecordsAffected;


	m_strSql.Format(_T("use master restore database ") +str_database+ _T(" from disk = '") +str_path_filename+ _T("' with recovery"));
	bool bSuccess = pMyConnect->Execute(_bstr_t(m_strSql),&RecordsAffected,adCmdText);

	if ( !bSuccess )
	{
		AfxMessageBox(_T("恢复失败!"));
	}
	else
	{
		AfxMessageBox(_T("恢复成功!"));
		
	}
	m_strSql.Format(_T("use ") +str_database );
	pMyConnect->Execute(_bstr_t(m_strSql),&RecordsAffected,adCmdText);


	/*_CommandPtr m_pCommand = NULL;//还是智能指针
	

	HRESULT hr = m_pCommand.CreateInstance(__uuidof(Command));
	m_pCommand->CommandText = "killspid";
	m_pCommand->CommandType = adCmdStoredProc;

	_ParameterPtr m_pParam;
	m_pParam.CreateInstance(__uuidof(Connection));
	

	m_pParam = m_pCommand->CreateParameter(_T("dbname"),adVarChar,adParamInput,20,(_variant_t)_T("mytest"));//给参数设置各属性
	m_pCommand->Parameters->Append(m_pParam);//加入到Command对象的参数集属性中
	

    m_pCommand->ActiveConnection = pMyConnect;//设置连接,别忘了啊
	try
	{
		m_pCommand->Execute(NULL,NULL,adCmdStoredProc); 
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		//printf("%s\n", (LPCTSTR)e.Description());
	}

	_CommandPtr m_pCommand1 = NULL;//还是智能指针
	m_pCommand1->CommandText = "pr_restore_db1";
	m_pCommand1->CommandType = adCmdStoredProc;

	HRESULT hr1 = m_pCommand1.CreateInstance(__uuidof(Command));

    m_pCommand1->ActiveConnection = pMyConnect;//设置连接,别忘了啊
	try
	{
		m_pCommand1->Execute(NULL,NULL,adCmdStoredProc); 
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		//printf("%s\n", (LPCTSTR)e.Description());
	}

*/


	OnOK();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -