描述:
谁能给个源码啊。
不用MFC类库。
解决方案1:
用MFC AppWizard新建一个工程:step1 单文档 step2 完成
然后
1:在"StdAfx.h"添加一行:#import "C:\Program Files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2:给CView类添加成员变量:
_ConnectionPtr pConn;
_RecordsetPtr pRst;
3: 在CView类的构造函数里面添加:
AfxOleInit();
pConn.CreateInstance(__uuidof(Connection));
pRst.CreateInstance(__uuidof(Recordset));
try
{
pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cidian.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库cidian.mdb是否在当前路径下!");
return;
}
4:在CView析构函数里面添加
try
{
pRst.Release();
pConn.Release();
}
catch(_com_error e)
{
AfxMessageBox("数据库关闭失败!");
}
5:可以在CView成员函数里面用了。举例:
CString cmdtext="select * from words where word='"+查询词语+"'";
try
{
pRst=pConn->Execute((_bstr_t)cmdtext,NULL,adCmdText);
if(!pRst->adoEOF)
{
//如果找到了,就......
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
6:最后建议你尽量使用MFC里的ODBC进行数据库编程,方便很多,需要编写的代码量很少
vc6对Ado编程支持的不好
如果真的想学Ado就直接学Ado.net吧,比Ado有很大改进,也是很方便的
不过当然要在.net环境下了
ADO
http://www.vckbase.com/document/finddoc.asp?keyword=ADO