vc++ 基本对话框类型的MFC从哪里连接数据库

2024-12-16 22:56:00
推荐回答(1个)
回答1:

不要用odbc,用ado
第一步
// 加入ADO支持库
#import "c:\program files\common files\system\ado\msado15.dll"\
no_namespace \
rename ("EOF", "adoEOF")

#pragma comment(lib, "shlwapi.lib")

第二步:
#pragma once
#include
#include
#include

class CDBManager
{
public:
CDBManager(void);
public:
~CDBManager(void);

void Init(CString ip, CString dbName, CString dbUser, CString dbPass, int type);//初始化数据库连接设置

BOOL Test();//测试数据库连接是否正常

BOOL Update(CString sql);//执行insert、update和delete语句

BOOL Query(CString sql, CObList& result );//得到查询的记录集合

public:
CString m_dbstr;//数据库ado连接字符串
};

2、DBManager.cpp
#include "StdAfx.h"
#include "DBManager.h"
#include "Config.h"

CDBManager::CDBManager(void)
{
m_dbstr = _T("");
}

CDBManager::~CDBManager(void)
{
}

void CDBManager::Init(CString ip, CString dbName, CString dbUser, CString dbPass, int type)
{
if(type == DB_SERVER_SQL_TYPE)
m_dbstr.Format(_T("Provider='SQLOLEDB';Data Source='%s';Initial Catalog='%s';User ID='%s';Password='%s';"),ip,dbName,dbUser,dbPass);
else
m_dbstr.Format(_T("Provider='OraOLEDB.Oracle';Data Source='%s_%s';User ID='%s';Password='%s';"),dbName,ip,dbUser,dbPass);
}

BOOL CDBManager::Test()
{
BOOL flag = FALSE;
::CoInitialize(NULL);
_ConnectionPtr m_pConnection = NULL;
try
{
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect= m_dbstr;

m_pConnection->Open(strConnect,L"",L"",adModeUnknown);
if(m_pConnection!=NULL)
flag = TRUE;
if(m_pConnection->State)
m_pConnection->Close();
}
catch(_com_error e)
{
flag = FALSE;
}
::CoUninitialize();

return flag;
}

BOOL CDBManager::Update(CString sql)
{
BOOL flag = FALSE;

::CoInitialize(NULL);
_ConnectionPtr m_pConnection = NULL;
try
{
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect= m_dbstr;

m_pConnection->Open(strConnect,L"",L"",adModeUnknown);
if(m_pConnection==NULL)
return FALSE;

_bstr_t bstr_sql = sql;
m_pConnection->Execute(bstr_sql,NULL,1);
flag = TRUE;
}
catch(_com_error e)
{
flag = FALSE;
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();

return flag;
}

BOOL CDBManager::Query(CString sql, CObList& result)
{
BOOL flag = TRUE;

::CoInitialize(NULL);
_ConnectionPtr m_pConnection = NULL;
_RecordsetPtr m_pRecordset = NULL;
try
{
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect= m_dbstr;//"Provider=SQLOLEDB;Server=192.168.1.64;Database=mserver; uid=mserver; pwd=mserver;";

m_pConnection->Open(strConnect,L"",L"",adModeUnknown);
if(m_pConnection==NULL)
return FALSE;
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open("select * from T_Mediaserver",m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);

int columnsSize = m_pRecordset->Fields->GetCount();

while (!m_pRecordset->EndOfFile)
{
result.AddTail(new CMapStringToString);
for(int j = 0; j < columnsSize; j++)
{
FieldPtr fPtr = m_pRecordset->Fields->GetItem((long)j);
_variant_t vName = fPtr->Name;
_variant_t vValue = fPtr->Value;
CString name((WCHAR*)(_bstr_t)vName);
CString value = _T("");
if(vValue.vt != VT_NULL)
value.Format(_T("%s"),(WCHAR*)(_bstr_t)vValue);
((CMapStringToString*)result.GetTail())->SetAt(name,value);
}
m_pRecordset->MoveNext(); ///移到下一条记录
}
m_pRecordset->Close(); // 关闭记录集
}
catch(_com_error e)
{
flag = FALSE;
}
if(m_pRecordset->State)
m_pRecordset->Close();
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();

return flag;
}

备注:
1、需要在stdafx.h中加入
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile") //ADO数据库连接
2、连接Oracle数据库前需要先在客户机配置oracle客户端。