4,011
社区成员
发帖
与我相关
我的任务
分享
void CEmailManagerDlg::OnClickedBtnconn()
{
// TODO: 在此添加控件通知处理程序代码
// TODO: Add your control notification handler code here
if (!UpdateData())return;
//判断数据库是否连接
if (m_db.IsOpen())
{
AfxMessageBox(_T("数据库已连接"));
return;
}
//数据库配置参数不能为空
if (m_strDBSource.IsEmpty() || m_strUserName.IsEmpty() || m_strPassword.IsEmpty())
{
AfxMessageBox(_T("数据库配置参数不能为空"));
return;
}
//创建连接字符串
CString strConnect;
strConnect.Format(_T("DSN=%s; UID=%s; PWD=%s"), m_strDBSource, m_strUserName, m_strPassword);
//打开数据库的连接,并且捕获异常
TRY{
m_db.OpenEx(strConnect,CDatabase::noOdbcDialog);
}
CATCH(CDBException, ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
END_CATCH
}
//#pragma once
// ADOConn.h: interface for the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF") rename("BOF", "adoBOF")
// #import "msado15.dll" no_namespace rename("EOF", "adoEOF")
#if !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
#define AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ADOConn
{
// 定义变量
public:
//添加一个指向Connection对象的指针:
_ConnectionPtr m_pConnection;
//添加一个指向Recordset对象的指针:
_RecordsetPtr m_pRecordset;
// 定义方法
public:
ADOConn();
virtual ~ADOConn();
// 初始化—连接数据库
void OnInitADOConn();
// 执行查询
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
// 执行SQL语句,Insert Update _variant_t
BOOL ExecuteSQL(_bstr_t bstrSQL);
void ExitConnect();
};
#endif // !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ADOConn.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库环境
::CoInitialize(NULL);
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
m_pConnection->Open("DSN=ASCIIGSZ;Server= localhost;Database=gszp17010", "root", "123456", adModeUnknown);
}
// 捕捉异常
catch (_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
}
// 执行查询
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
// 连接数据库,如果Connection对象为空,则重新连接数据库
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;
}
// 执行SQL语句,Insert Update _variant_t
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
// _variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if (m_pConnection == NULL)
OnInitADOConn();
// Connection对象的Execute方法:(_bstr_t CommandText,
// VARIANT * RecordsAffected, long Options )
// 其中CommandText是命令字串,通常是SQL命令。
// 参数RecordsAffected是操作完成后所影响的行数,
// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
// adCmdProc-存储过程;adCmdUnknown-未知
m_pConnection->Execute(bstrSQL, NULL, 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();
}
这个到底算是ADO还是ODBC。
如果不是ADO,此处却引用ADO的动态库。
如果不是ODBC,教程里面却要配置ODBC。
××××××××××××
我的操作系统是WIn7 64位中文旗舰版。 Mysql是mysql-5.7.10-winx64 ,VS是 2015 企业版。
出现的问题和楼主是一样的 驱动程序和应用程序之间的体系结构不匹配