代码如下:
BOOL bConnected = FALSE;
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
HRESULT hr;
try
{
hr = pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
_bstr_t bstrConnection(L"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\works\\VC\\db1.mdb;");
hr = pConnection->Open(bstrConnection, _bstr_t(L""), _bstr_t(L""), adModeUnknown);
if(SUCCEEDED(hr))
{
bConnected = TRUE;
}
}
if(bConnected)
{
_bstr_t bstrQuery(L"select * from table1");
_variant_t recordAffect(0L); pRecordset.CreateInstance("ADODB.Recordset");
pRecordset = pConnection->Execute(bstrQuery, &recordAffect, adOptionUnspecified);
}
}
catch (_com_error &e)
{
TRACE("com error.");
}
catch(...)
{
TRACE("unknown error.");
}
结果Debug栏错误信息栏如下所示:
Loaded 'C:\Program Files\Common Files\System\ado\msader15.dll', no matching symbolic information found.
而我如果用ListCtrl来显示则显示正常且没有错误提示。到ado目录下也可以看到这个DLL。究竟是什么问题呢?
另外,我在家里的机器上试时,可以正常连接并返回正确的结果:因为用ListView可以正常显示;但是当我想把它用DataGrid表示出来的时候就出现问题了:
pRecordset.CreateInstance("ADODB.Recordset");
pRecordset = pConnection->Execute(bstrQuery, &recordAffect, adOptionUnspecified);
m_dataGrid.SetRefDataSource((LPUNKNOWN)pRecordset);
出现了和上面同样的错误,为什么呢?
BOOL bConnected = FALSE;
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
HRESULT hr;
try
{
hr = pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
_bstr_t bstrConnection(L"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\works\\VC\\db1.mdb;");
hr = pConnection->Open(bstrConnection, _bstr_t(L""), _bstr_t(L""), adModeUnknown);
if(SUCCEEDED(hr))
{
bConnected = TRUE;
}
}
if(bConnected)
{
_bstr_t bstrQuery(L"select * from table1");
_variant_t recordAffect(0L); pRecordset.CreateInstance("ADODB.Recordset");
pRecordset = pConnection->Execute(bstrQuery, &recordAffect, adOptionUnspecified);
}
}
catch (_com_error &e)
{
TRACE("com error.");
}
catch(...)
{
TRACE("unknown error.");
}
结果Debug栏错误信息栏如下所示:
Loaded 'C:\Program Files\Common Files\System\ado\msader15.dll', no matching symbolic information found.
而我如果用ListCtrl来显示则显示正常且没有错误提示。到ado目录下也可以看到这个DLL。究竟是什么问题呢?
另外,我在家里的机器上试时,可以正常连接并返回正确的结果:因为用ListView可以正常显示;但是当我想把它用DataGrid表示出来的时候就出现问题了:
pRecordset.CreateInstance("ADODB.Recordset");
pRecordset = pConnection->Execute(bstrQuery, &recordAffect, adOptionUnspecified);
m_dataGrid.SetRefDataSource((LPUNKNOWN)pRecordset);
出现了和上面同样的错误,为什么呢?
_variant_t recordAffect(0L);
pRecordset.CreateInstance("ADODB.Recordset");
pRecordset = pConnection->Execute(bstrQuery, &recordAffect, adOptionUnspecified);
这句不是在连接table了吗?如果不是又怎么连呢?
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset1;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection.CreateInstance("ADODB.Connection");
m_pConnection->PutCursorLocation(adUseClient); //必须加上此句!!!!//连接数据库
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\testrecord.mdb","","",adModeUnknown);
//wndSplash.OnPaint();
wndSplash.UpdateWindow();
wndSplash.ShowMessage("正在连接数据库......");
Sleep(1000); }
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库testrecord.mdb是否在当前路径下!");
wndSplash.UpdateWindow();
wndSplash.ShowMessage("数据库连接失败");
Sleep(1000);
return FALSE;
}
//wndSplash.Create(IDB_BITMAP5);
wndSplash.UpdateWindow();
wndSplash.ShowMessage("数据库连接成功!");
m_pRecordset1.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset1->Open("SELECT * FROM 水泥", // 查询DemoTable表中所有字段
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
我用的是datagrid 没有你说的那个错误的阿