ADO 的方法怎么连ACCess数据库!
贴个程序出来阿 问题点数:100、回复次数:3Top
1 楼baby_zhx(郁闷)回复于 2002-06-06 11:04:26 得分 100
////////////连接数据库//////////////
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=expert.mdb","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}Top
2 楼masterz(www.fruitfruit.com)回复于 2002-06-06 11:07:03 得分 0
//=====================Open Access
#include "stdafx.h"
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
int main(int argc, char* argv[])
{
printf("Use ADO to open C:\\tmp\\test.mdb database file!\n");
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
pConn->Open("PROVIDER=MSDASQL;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\tmp\\test.mdb;UID=;PWD=aaa;"
//pConn->Open("Provider= Microsoft.Jet.OLEDB.4.0;Data Source=C:\\tmp\\test.mdb;"//this is also OK
,"","",adConnectUnspecified);
//Open "users" table
pRst->Open("users", _variant_t((IDispatch *) pConn, true),
adOpenStatic, adLockReadOnly, adCmdTable);
FieldsPtr fds=pRst->GetFields();
printf("printf field name of the table\n");
for(int i=0;i<fds->GetCount();i++)
{
FieldPtr fd=fds->GetItem(_variant_t(short(i)));
printf("%s ",(LPCTSTR)fd->GetName());
}
printf("\n");
pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}Top
3 楼wistaria(听风听雨)回复于 2002-06-06 11:13:55 得分 0
http://www.tech521.com/show_data.asp?tid=1699Top




