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<DEMO>& 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; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值