MFC

#include "stdafx.h"
#include "DateOperator.h"


CDateOperator::CDateOperator(void)
{
	try
	{
		HRESULT hr =  m_pConnection.CreateInstance("ADODB.Connection");
		if(FAILED(hr))
		{
			AfxMessageBox(L"Create Instance failed!");
			m_pConnection = NULL;
		}
	}

	catch(_com_error &e)
	{
	}
}


CDateOperator::~CDateOperator(void)
{
	if(m_pConnection)
	{
		try
		{
			HRESULT hr =  m_pConnection->Close();
			
		}

		catch(_com_error &e)
		{
		}
	}
	m_pConnection.Release();
	m_pConnection = NULL;
	
}
BOOL CDateOperator::OpenDateBase(CString strDbName, CString strUserName,CString strUserPwd)
{

	if(NULL ==	m_pConnection){
		AfxMessageBox(L"Create Instance failed!");
		return FALSE;
	}
	if(m_pConnection){
		
		
		try
		{
			
			const _bstr_t strSRC= "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mydb;Data Source=LI5CH\\SQLEXPRESS";
//			CString strConnectionName;
//			strConnectionName.Format(L"Driver= SQL Server;Server = (LI5CH\SQLEXPRESS); Database=%s;UID=%s;PWD=%s",
//				strDbName,strUserName,strUserPwd);
//			HRESULT hr = m_pConnection->Open(_bstr_t(strConnectionName.GetBuffer(0)),L"",L"",-1);
			HRESULT hr = m_pConnection->Open(strSRC,L"",L"",-1);
		}
		catch(_com_error &e)
		{
			CString errormsg;
			errormsg.Format(L"关闭连接——操作错误!\r\n错误信息:%s",e.ErrorMessage());
			AfxMessageBox(errormsg);
			
		}

	}
	return TRUE; 	

}
BOOL CDateOperator::ExcuteSQL(CString strSQL){
	if(NULL ==	m_pConnection) return FALSE;
	try
		{
		
			HRESULT hr = m_pConnection->Execute(_bstr_t(strSQL),NULL,-1);
		}
		catch(_com_error &e)
		{
			return FALSE;
		}
		return TRUE;
}


BOOL CDateOperator::Select_db(vector& vecObjUser)
{
	if(NULL == m_pConnection) return FALSE;
	_RecordsetPtr m_pRecordset;
	HRESULT hr = m_pRecordset.CreateInstance(L"ADODB.Recordset");
	if(FAILED(hr)) return FALSE;
	vecObjUser.clear();
	CString strSQL = L"SELECT User_ID, User_Name FROM fate";
	hr = m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
	if(FAILED(hr)){
		m_pRecordset.Release();
		return FALSE;
	}
	VARIANT_BOOL bRet = m_pRecordset->GetadoEOF();
	while(!bRet){
		_variant_t varUserID = m_pRecordset->GetCollect(L"User_ID");
		_variant_t varUserName = m_pRecordset->GetCollect(L"User_Name");
		DEMO objUser;
		objUser.User_ID=varUserID.intVal;

		_tcscpy(objUser.User_Name,(TCHAR*)(_bstr_t)varUserName);
		vecObjUser.push_back(objUser);
		m_pRecordset->MoveNext();
		bRet = m_pRecordset->GetadoEOF();

	}
	m_pRecordset->Close();
	m_pRecordset.Release();
	return TRUE; 
	
}
BOOL CDateOperator::Insert_db(DEMO objuser)
{
	if(NULL == m_pConnection) return FALSE;
	_RecordsetPtr m_pRecordset;
	HRESULT hr = m_pRecordset.CreateInstance(L"ADODB.Recordset");
	if(FAILED(hr)) return FALSE;
	
	CString strSQL = L"SELECT User_ID, User_Name FROM fate";
	hr = m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
	if(FAILED(hr)){
		m_pRecordset.Release();
		return FALSE;
	}
	try
	{
		m_pRecordset->AddNew();
	}
	catch(_com_error &e)
	{
		m_pRecordset->Close();
		m_pRecordset.Release();
		return FALSE;
	}
//	byte byData[10000];
	try
	{
		m_pRecordset->PutCollect("User_ID",_variant_t(objuser.User_ID));
		m_pRecordset->PutCollect("User_Name",_variant_t(objuser.User_Name));
	}
	catch(_com_error &e)
	{
		m_pRecordset->Close();
		m_pRecordset.Release();
		return FALSE;
	}
	m_pRecordset->Update();
	m_pRecordset->Close();
	m_pRecordset.Release();
	return TRUE; 
}
BOOL CDateOperator::update_db(DEMO objuser)
{
	if(NULL == m_pConnection) return FALSE;
	_RecordsetPtr m_pRecordset;
	HRESULT hr = m_pRecordset.CreateInstance(L"ADODB.Recordset");
	if(FAILED(hr)) return FALSE;
	CString strSQL;
	strSQL.Format(L"SELECT User_ID, User_Name FROM fate WHERE User_ID=%d", objuser.User_ID);
	hr = m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
	if(FAILED(hr)){
		m_pRecordset.Release();
		return FALSE;
	}

//	byte byData[10000];
	try
	{
		m_pRecordset->PutCollect("User_Name",_variant_t(objuser.User_Name));
	}
	catch(_com_error &e)
	{
		m_pRecordset->Close();
		m_pRecordset.Release();
		return FALSE;
	}
	m_pRecordset->Update();
	m_pRecordset->Close();
	m_pRecordset.Release();
	return TRUE; 
}

你可能感兴趣的:(MFC学习)