VS2008 MFC连接SQL数据库
天魔雷帝 2011-10-05 01:29:19 VS2008连接数据库的具体步骤是什么?我看网上有说在控制面板的数据源里面先添加数据源的,以前做JAVA的时候连数据库只要写好连接字符串然后用连接类操作就行,没有在数据源里面添加啊!
下面是我写的代码
_CommandPtr m_ptrCommand; //命令对象
_RecordsetPtr m_ptrRecordset; //记录集对象
_ConnectionPtr m_ptrConnection; //数据库对象
//创建对象
m_ptrCommand.CreateInstance(__uuidof(Command));
m_ptrRecordset.CreateInstance(__uuidof(Recordset));
m_ptrConnection.CreateInstance(__uuidof(Connection));
try{
m_ptrConnection->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=eForceCon;Data Source=JANE-PC\\SQLEXPRESS";
m_ptrConnection->Open("","","",adModeUnknown);
m_ptrRecordset=m_ptrConnection->Execute("use eForceCon select * from data1",NULL,adCmdText);
m_ptrRecordset->GetCollect("Time");
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( "连接数据库发生异常! \r \n错误信息:%s",e.ErrorMessage());
AfxMessageBox(strError);//显示错误信息
}
到m_ptrRecordset->GetCollect("Time");这一句就有问题,捕获的异常是:未知错误,求解释啊。高手帮忙啊,以上代码有什么错误呢?或者直接告诉我正确的做法也行啊,刚学,望指点