ADOConn.cpp
上传用户:xz12345
上传日期:2007-09-12
资源大小:7437k
文件大小:2k
- // ADOConn.cpp: implementation of the ADOConn class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "工资.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- ADOConn::ADOConn()
- {
- }
- ADOConn::~ADOConn()
- {
- }
- void ADOConn::OnInitADOConn()
- {
- //初始化OLE/COM库环境
- //public:
- ::CoInitialize(NULL);
- try
- {
- m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
- //m_pConnection.CreatInsancet
- //_bstr_t strConnect="Provider=SQLOLEDB;Sever=.;DataBase=airchina;uid=sa;pwd=;";
- _bstr_t strConnect="Provider=SQLOLEDB;Sever=.;DataBase=salarynew;Integrated Security=SSPI;";
-
- m_pConnection->Open(strConnect,"","",adModeUnknown);
- }
- catch(_com_error e){
- AfxMessageBox(e.Description());
- }
- }
- _RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
- {
- try
- {
- //连接数据库
- if(m_pConnection==NULL)
- OnInitADOConn();
- //创建记录集
- m_pRecordset.CreateInstance(__uuidof(Recordset));
- //取得表中记录
- m_pRecordset->Open( bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
- }
- catch(_com_error e)
- {
- AfxMessageBox(e.Description());
- }
- return m_pRecordset;//返回记录集
- }
- BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
- {
- _variant_t RecordsAffected;
- try
- {
- if(m_pConnection==NULL)
- OnInitADOConn();
- m_pConnection->Execute(bstrSQL,0,adCmdText);
- return true;
- }
- catch(_com_error e)
- {
- AfxMessageBox(e.Description());
- return false;
- }
- }
- void ADOConn::ExitConnect()
- {
- //关闭记录集和链接
- if(m_pRecordset!=NULL)
- m_pRecordset->Close();
- m_pConnection->Close();
- ::CoUninitialize();//释放环境
- }