c++ 中连接到access的函数
来源:互联网 发布:淘宝hd v5.0.1 编辑:程序博客网 时间:2024/06/12 01:14
在文件头需要添加下面这段:
#import "c:\program files\common files\system\ado\msado15.dll"no_namespace rename("EOF", "adoEOF")
bool linkAccess(int num)
{
//连接到数据库中,并输出数据库中的所有叶片名称
_ConnectionPtr pConnection;
if(pConnection.CreateInstance(__uuidof(Connection)) !=S_OK)
{
return FALSE;
}
_bstr_t strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=leaf.mdb";
if(pConnection->Open(strConn, "", "",adModeUnknown) == S_OK)
{
//连接数据库成功
_RecordsetPtr pRecordset;
if(pRecordset.CreateInstance(__uuidof(Recordset)) !=S_OK)
{
pConnection->Close();
return FALSE;
}
//下面四行练习的是如何将int->string
char buffer [33];
itoa(num,buffer,33);
string s(buffer);
string str="SELECT * from leaf where ID="+s;
//下面一行练习如何将string 转为char*
char *orig=const_cast<char*>(str.c_str());
//下面一行练习如何将char* 转为_bstr_t
_bstr_t strSql(orig);
//上面这么多行的目的就是将num添加到string中,变为_bstr_t 变量
HRESULT hr = pRecordset->Open(strSql,_variant_t((IDispatch*)pConnection, TRUE), adOpenUnspecified,adLockUnspecified, adCmdUnknown);
if(hr != S_OK)
{
pConnection->Close();
return FALSE;
}
_variant_t vt;
pRecordset->MoveFirst();
while(!pRecordset->adoEOF)
{
vt =pRecordset->Fields->GetItem("名称")->Value;
cout<<_com_util::ConvertBSTRToString((_bstr_t)vt)<<endl;
vt =pRecordset->Fields->GetItem("介绍")->Value;
cout<<_com_util::ConvertBSTRToString((_bstr_t)vt)<<endl;
pRecordset->MoveNext();
}
pConnection->Close();
return TRUE;
}
else
{
cout << "数据库连接失败!"<< endl;
return FALSE;
}
}
0 0
- c++ 中连接到access的函数
- c++ 中连接到access的函数
- MS SQL和Access中使用函数的区别
- C语言中access函数的使用
- C语言中access函数
- C语言中access函数
- C语言中access函数
- C语言中access函数
- C语言中access函数
- C语言中access函数
- linux c中access函数
- 通过access.oledb连接到excel中
- C中判断文件是否存在的access函数
- EntitySpaces2009中连接Access的连接设置
- 连接到access 2007
- PHP连接ACCESS(函数)
- 我终于连接access到jsp中啦!
- C语言里,main 函数中 r…
- Eclipse下安装Android开发环境:NDK…
- Eclipse下安装Android开发环境:NDK…
- 我在使用SurfaceView与Camera开发…
- 我在使用SurfaceView与Camera开发…
- c++ 中连接到access的函数
- c++ 中连接到access的函数
- c# WinForm调用控制台
- c# WinForm调用控制台
- C# EXE程序运行后 怎样才能自动关…
- C# EXE程序运行后 怎样才能自动关…
- c#的文件读取和写入方法类
- c#的文件读取和写入方法类
- InputStreamReader的中文乱码的问…
- InputStreamReader的中文乱码的问…