1、DbHelper.h
// 数据库辅助函数头文件 // add by jzh 2009-07-06 #pragma once class DbHelper { public: // 定义ADO连接变量指针 _ConnectionPtr m_pConnection; // 命令变量指针 _CommandPtr m_pCommand; // 记录集变量指针 _RecordsetPtr m_pRecordset; public: DbHelper(void); ~DbHelper(void); // 初始化连接数据库 void InitConn(); // 执行查询 _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL); // 执行SQL语句 Insert Update BOOL ExecuteSQL(_bstr_t bstrSQL); // 获取流水号 CString GetSerialNo(CString sTableName, CString sDataType, CString sBillType, CString sFormat); // 释放资源 void ExitConn(); };
2、DbHelper.cpp
// 数据库辅助函数实现文件 // add by jzh 2009-07-06 #include "StdAfx.h" #include "DbHelper.h" DbHelper::DbHelper(void) { } DbHelper::~DbHelper(void) { } // ------------------------------------------------------------------ // 初始化连接数据库 // add by jzh 2009-07-06 // ------------------------------------------------------------------ void DbHelper::InitConn() { ::CoInitialize(NULL); //初始化 OLE/COM 库环境 try { // 创建 Connection 对象 m_pConnection.CreateInstance("ADODB.Connection"); _bstr_t sConn = ""; if (g_sDBFlag == "0") // Access { sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + g_sExePath + "\\" + g_sAccessFile; } else if (g_sDBFlag == "1") // SQLServer { sConn = "Provider=SQLOLEDB; Server=" + g_sDBServerIP + "; UID=" + g_sUserName + "; PWD=" + g_sPassword + "; DataBase=" + g_sDataBase + ";"; } else if (g_sDBFlag == "2") // Oracle { sConn = "Provider=OraOLEDB.Oracle; User ID=" + g_sOraUser + "; Password=" + g_sOraPWD + "; Data Source=" + g_sOraDataSource + "; PLSQLRSet=1;"; //sConn = "Provider=OraOLEDB.Oracle; User ID=scott; Password=tiger; Data Source=HTCRM; PLSQLRSet=1;"; } m_pConnection->Open(sConn, "", "", adModeUnknown); } catch(_com_error e) { AfxMessageBox(e.Description()); //显示错误信息 } } // ------------------------------------------------------------------ // 执行查询 // add by jzh 2009-07-06 // ------------------------------------------------------------------ _RecordsetPtr& DbHelper::GetRecordSet(_bstr_t bstrSQL) { try { // 连接数据库,如果Connection对象为空,则重新连接数据库 if (m_pConnection == NULL) InitConn(); // 创建记录集对象 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; } // ------------------------------------------------------------------ // 执行SQL语句 Insert Update // add by jzh 2009-07-06 // ------------------------------------------------------------------ BOOL DbHelper::ExecuteSQL(_bstr_t bstrSQL) { try { // 连接数据库,如果Connection对象为空,则重新连接数据库 if (m_pConnection == NULL) InitConn(); // 执行语句 m_pConnection->Execute(bstrSQL, NULL, adCmdText); return TRUE; } catch(_com_error e) { AfxMessageBox(e.Description()); //显示错误信息 return FALSE; } } // ------------------------------------------------------------------ // 获取流水号 // add by jzh 2009-08-05 // ------------------------------------------------------------------ CString DbHelper::GetSerialNo(CString sTableName, CString sDataType, CString sBillType, CString sFormat) { CString sValue; try { // 连接数据库,如果Connection对象为空,则重新连接数据库 if (m_pConnection == NULL) InitConn(); // 实例化 m_pCommand.CreateInstance("ADODB.Command"); // 设置连接 m_pCommand->ActiveConnection = m_pConnection; // 存储过程为sp_GetSerialNo m_pCommand->CommandText = "sp_GetSerialNo"; // 输入参数 表名 _ParameterPtr pParamTableName; pParamTableName.CreateInstance("ADODB.Parameter"); //pParamTableName->Name= "sntablename"; // 所用存储过程参数名称 //pParamTableName->Type= adChar; // 参数类型 //pParamTableName->Size= 32; // 参数大小 //pParamTableName->Direction= adParamInput; // 表明是输入参数 //pParamTableName->Value= _variant_t(""); // 传入值 pParamTableName = m_pCommand->CreateParameter("sntablename", adChar, adParamInput, sTableName.GetLength(), (_variant_t)sTableName); //给参数设置各属性 m_pCommand->Parameters->Append(pParamTableName); // 输入参数 单号数据类型(0--整型;1--字符型,但不按日期产生;2--字符型,但按日期产生流水号) _ParameterPtr pParamDataType; pParamDataType.CreateInstance("ADODB.Parameter"); pParamDataType = m_pCommand->CreateParameter("idatatype", adInteger, adParamInput, sDataType.GetLength(), (_variant_t)sDataType); // 给参数设置各属性 m_pCommand->Parameters->Append(pParamDataType); // 输入参数 缀单据类型 2位 _ParameterPtr pParamBillType; pParamBillType.CreateInstance("ADODB.Parameter"); pParamBillType = m_pCommand->CreateParameter("sbilltype", adVarChar, adParamInput, sBillType.GetLength(), (_variant_t)sBillType); // 给参数设置各属性 m_pCommand->Parameters->Append(pParamBillType); // 输入参数 传入流水号格式化位数,如4位传入'0000' _ParameterPtr pParamFormat; pParamFormat.CreateInstance("ADODB.Parameter"); pParamFormat = m_pCommand->CreateParameter("sformat", adVarChar, adParamInput, sFormat.GetLength(), (_variant_t)sFormat); // 给参数设置各属性 m_pCommand->Parameters->Append(pParamFormat); // 输出参数 取得单号 _ParameterPtr pParamSN; pParamSN.CreateInstance("ADODB.Parameter"); pParamSN = m_pCommand->CreateParameter("maxserialno", adVarChar, adParamOutput, 20); // 给参数设置各属性 m_pCommand->Parameters->Append(pParamSN); // 执行 m_pCommand->Execute(NULL, NULL, adCmdStoredProc); sValue = (char*)_bstr_t(pParamSN->Value); // 释放资源 pParamTableName->Release(); pParamBillType->Release(); pParamFormat->Release(); pParamSN->Release(); m_pConnection->Close(); CoUninitialize(); return sValue.Trim(); } catch(_com_error e) { AfxMessageBox(e.Description()); //显示错误信息 return ""; } } // ------------------------------------------------------------------ // 退出 // add by jzh 2009-07-06 // ------------------------------------------------------------------ void DbHelper::ExitConn() { if (m_pRecordset != NULL) m_pRecordset->Close(); // 释放资源 m_pConnection->Close(); ::CoUninitialize(); //关闭 OLE/COM 库 并释放资源 }
3、调用
// ProCAppDlg.cpp : 实现文件 // add by jzh 2009-07-05 #include "DbHelper.h" // 数据库辅助函数类 // --------------------------------------------------------------------------- // 测试 // add by jzh 2009-07-05 // --------------------------------------------------------------------------- void CProCAppDlg::OnBnClickedOk() { //ADO测试 _variant_t vObj; CString sSQL; CString sLogDesc; CString sID; DbHelper m_DbHelper; _RecordsetPtr m_pRecordset; sLogDesc = "测试记录"; sID = "3"; //sSQL = " insert into LogInfo(LogDesc) values('"+ sLogDesc + "') "; //sSQL = " delete from LogInfo where ID="+ sID; //sSQL = " select ID, LogDesc from LogInfo where ID="+ sID; //sSQL = " select ID, LogDesc from LogInfo "; sSQL = " select USERID, USERNAME from FSYS_USER "; m_DbHelper.InitConn(); //m_DbHelper.ExecuteSQL(_bstr_t(sSQL)); m_pRecordset = m_DbHelper.GetRecordSet(_bstr_t(sSQL)); try { if(!m_pRecordset->BOF) { m_pRecordset->MoveFirst(); } else { AfxMessageBox("表内数据为空"); return; } // 读取库中各字段 while(!m_pRecordset->adoEOF) { // ID vObj = m_pRecordset->GetCollect("USERID"); if(vObj.vt != VT_NULL) sID = (LPCSTR)_bstr_t(vObj); // LogDesc vObj = m_pRecordset->GetCollect("USERNAME"); if(vObj.vt != VT_NULL) sLogDesc = (LPCSTR)_bstr_t(vObj); MessageBox(sID + sLogDesc); // 下一条 m_pRecordset->MoveNext(); } } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); } m_DbHelper.ExitConn(); }
您好,您这个是用ADO连接的吗?
您好。是用ado连接的。