_ConnectionPtr m_pConnection;
m_pConnection.CreateInstance(__uuidof(Connection));
CString strSQL;
strSQL=_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=guest_manager;User Id=root;Password=1046;");
try
{
m_pConnection->Open((_bstr_t)strSQL,"","",adModeUnknown);
}
catch(_com_error e)
{
CString strError;
strError.Format(_T("警告:创建连接发生异常。错误信息:%s"),e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
使用的是MySQL 5.5 complete 安装;
VS2008配置
项目属性-》附加包含目录:"C:\Program Files\MySQL\MySQL Server 5.5\lib\debug";"C:\Program Files\MySQL\MySQL Server 5.5\include"
工具-》选项-》项目和解决方案-》VC++目录-》包含文件:C:\Program Files\MySQL\MySQL Server 5.5\include
工具-》选项-》项目和解决方案-》VC++目录-》库文件:C:\Program Files\MySQL\MySQL Server 5.5\lib\debug
编译无错,一连接数据库,就说:IDispatch #3149
求救啊,困扰我5天了,食不能安,夜不能寐啊!!!
7 个解决方案
#1
//初始化OLE/COM库环境
::CoInitialize(NULL);
// 初始化—连接数据库
void ADOConn::OnInitADOConn()
{
if(m_pConnection==NULL)
{
try
{
//创建Connection对象
m_pConnection.CreateInstance( "ADODB.Connection ");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
//连接Oracle数据库字符串
// _bstr_t strConnect = _T( "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=MyUsername;Pwd=MyPassword; ");
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
//连接SQL Server数据库的字符串
// _bstr_t strConnect = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=123;Initial Catalog=xx;Data Source=(local) ";
//执行数据连接
m_pConnection-> Open((_bstr_t)strConnectSQL, " ", " ",adModeUnknown);
//连接Access数据库的字符串
// _bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xx.mdb ";
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
}
}
::CoInitialize(NULL);
// 初始化—连接数据库
void ADOConn::OnInitADOConn()
{
if(m_pConnection==NULL)
{
try
{
//创建Connection对象
m_pConnection.CreateInstance( "ADODB.Connection ");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
//连接Oracle数据库字符串
// _bstr_t strConnect = _T( "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=MyUsername;Pwd=MyPassword; ");
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
//连接SQL Server数据库的字符串
// _bstr_t strConnect = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=123;Initial Catalog=xx;Data Source=(local) ";
//执行数据连接
m_pConnection-> Open((_bstr_t)strConnectSQL, " ", " ",adModeUnknown);
//连接Access数据库的字符串
// _bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xx.mdb ";
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
}
}
#3
照着1楼的改,没用。
2楼的我添加了libmysql.dll
2楼的我添加了libmysql.dll
#4
帮顶,没试过连mysql
#5
MYSQL的ADO连接字符串是这样的。
Provider=MSDASQL.1;Persist Security Info=True;Extended Properties="Driver=MySQL ODBC 5.1 Driver;SERVER=localhost;UID=root;PWD=123456;DATABASE=test;PORT=3306;CHARSET=gb2312;COLUMN_SIZE_S32=1"
异常捕获可以改成这样看是什么错误
Provider=MSDASQL.1;Persist Security Info=True;Extended Properties="Driver=MySQL ODBC 5.1 Driver;SERVER=localhost;UID=root;PWD=123456;DATABASE=test;PORT=3306;CHARSET=gb2312;COLUMN_SIZE_S32=1"
异常捕获可以改成这样看是什么错误
try
{
//你的ADO代码
}
catch (_com_error& e)
{
CString strMsg;
strMsg.Format(_T("错误描述:%s\n错误消息%s",
(LPCTSTR)e.Description(),
(LPCTSTR)e.ErrorMessage());
AfxMessageBox(strMsg);
}
#6
需要安装MySQL ODBC 5.1 Driver
#7
多谢大家帮忙,在各种煎熬下我该用Access数据库一下就连上了。
#1
//初始化OLE/COM库环境
::CoInitialize(NULL);
// 初始化—连接数据库
void ADOConn::OnInitADOConn()
{
if(m_pConnection==NULL)
{
try
{
//创建Connection对象
m_pConnection.CreateInstance( "ADODB.Connection ");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
//连接Oracle数据库字符串
// _bstr_t strConnect = _T( "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=MyUsername;Pwd=MyPassword; ");
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
//连接SQL Server数据库的字符串
// _bstr_t strConnect = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=123;Initial Catalog=xx;Data Source=(local) ";
//执行数据连接
m_pConnection-> Open((_bstr_t)strConnectSQL, " ", " ",adModeUnknown);
//连接Access数据库的字符串
// _bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xx.mdb ";
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
}
}
::CoInitialize(NULL);
// 初始化—连接数据库
void ADOConn::OnInitADOConn()
{
if(m_pConnection==NULL)
{
try
{
//创建Connection对象
m_pConnection.CreateInstance( "ADODB.Connection ");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
//连接Oracle数据库字符串
// _bstr_t strConnect = _T( "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=MyUsername;Pwd=MyPassword; ");
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
//连接SQL Server数据库的字符串
// _bstr_t strConnect = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=123;Initial Catalog=xx;Data Source=(local) ";
//执行数据连接
m_pConnection-> Open((_bstr_t)strConnectSQL, " ", " ",adModeUnknown);
//连接Access数据库的字符串
// _bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xx.mdb ";
// m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
}
}
#2
#3
照着1楼的改,没用。
2楼的我添加了libmysql.dll
2楼的我添加了libmysql.dll
#4
帮顶,没试过连mysql
#5
MYSQL的ADO连接字符串是这样的。
Provider=MSDASQL.1;Persist Security Info=True;Extended Properties="Driver=MySQL ODBC 5.1 Driver;SERVER=localhost;UID=root;PWD=123456;DATABASE=test;PORT=3306;CHARSET=gb2312;COLUMN_SIZE_S32=1"
异常捕获可以改成这样看是什么错误
Provider=MSDASQL.1;Persist Security Info=True;Extended Properties="Driver=MySQL ODBC 5.1 Driver;SERVER=localhost;UID=root;PWD=123456;DATABASE=test;PORT=3306;CHARSET=gb2312;COLUMN_SIZE_S32=1"
异常捕获可以改成这样看是什么错误
try
{
//你的ADO代码
}
catch (_com_error& e)
{
CString strMsg;
strMsg.Format(_T("错误描述:%s\n错误消息%s",
(LPCTSTR)e.Description(),
(LPCTSTR)e.ErrorMessage());
AfxMessageBox(strMsg);
}
#6
需要安装MySQL ODBC 5.1 Driver
#7
多谢大家帮忙,在各种煎熬下我该用Access数据库一下就连上了。