How to access database using C++ & ADO - 如何用C++和ADO访问数据库
2009-02-11 09:51
886 查看
一、在:CWinApp::InitInstance 函数中用AfxOleInit初始化 COM
if (!AfxOleInit())
{
AfxMessageBox("AfxOleInit Failed!");
return FALSE;
}
二、用 #Import 引入 ADO 对象 Dll:msado15.dll
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF", "adoEOF")
//Notice:这句话输入完毕后,VC就会在:程序生成目录中(Debug/Release)自动产生两个文件:
// msado15.tlh
// msado15.tli
三、使用 ADO 访问数据库
void CPrjADOAccessDatabaseTestDlg::OnbtnReadSqlServer()
{
// TODO: Add your control notification handler code here
CString strSql;
CString strMsg;
long lRecordCount;
_RecordsetPtr rs;
_ConnectionPtr conn;
_variant_t var;
//---------------------------------------------------------
rs.CreateInstance(__uuidof(Recordset));
conn.CreateInstance(__uuidof(Connection));
//---------------------------------------------------------
strSql = "select FirstName from dbo.Employees";
//连接数据库
conn->Open("Provider=SQLOLEDB;SERVER=127.0.0.1;DATABASE=AIS20070417160827;UID=sa;PWD=sa","","",-1);
//---------------------------------------------------------
//记录集取数
rs->CursorLocation = adUseClient;
rs->CursorType = adOpenForwardOnly;
rs->LockType = adLockReadOnly;
rs->Open("select * from t_User",conn.GetInterfacePtr(),adOpenForwardOnly,adLockReadOnly,adCmdText);
//遍历记录集
while(!rs->adoEOF)
{
var = rs->Fields->GetItem("FName")->Value;
strMsg = (LPCSTR)_bstr_t(var);
MessageBox(strMsg);
strMsg.ReleaseBuffer();
rs->MoveNext();
}
//清理战场,关闭对象
rs->Close();
conn->Close();
}
if (!AfxOleInit())
{
AfxMessageBox("AfxOleInit Failed!");
return FALSE;
}
二、用 #Import 引入 ADO 对象 Dll:msado15.dll
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF", "adoEOF")
//Notice:这句话输入完毕后,VC就会在:程序生成目录中(Debug/Release)自动产生两个文件:
// msado15.tlh
// msado15.tli
三、使用 ADO 访问数据库
void CPrjADOAccessDatabaseTestDlg::OnbtnReadSqlServer()
{
// TODO: Add your control notification handler code here
CString strSql;
CString strMsg;
long lRecordCount;
_RecordsetPtr rs;
_ConnectionPtr conn;
_variant_t var;
//---------------------------------------------------------
rs.CreateInstance(__uuidof(Recordset));
conn.CreateInstance(__uuidof(Connection));
//---------------------------------------------------------
strSql = "select FirstName from dbo.Employees";
//连接数据库
conn->Open("Provider=SQLOLEDB;SERVER=127.0.0.1;DATABASE=AIS20070417160827;UID=sa;PWD=sa","","",-1);
//---------------------------------------------------------
//记录集取数
rs->CursorLocation = adUseClient;
rs->CursorType = adOpenForwardOnly;
rs->LockType = adLockReadOnly;
rs->Open("select * from t_User",conn.GetInterfacePtr(),adOpenForwardOnly,adLockReadOnly,adCmdText);
//遍历记录集
while(!rs->adoEOF)
{
var = rs->Fields->GetItem("FName")->Value;
strMsg = (LPCSTR)_bstr_t(var);
MessageBox(strMsg);
strMsg.ReleaseBuffer();
rs->MoveNext();
}
//清理战场,关闭对象
rs->Close();
conn->Close();
}
相关文章推荐
- How to access database using ADO in VB6 - 在 VB6 中用ADO对象如何访问数据库
- How to access HTTP server using Telnet - 如何使用 Telnet 访问 Http 服务器
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- How to access SMTP/POP server using telnet - 用 Telnet 如何访问邮件服务器
- How to send mail using C++ & CAsyncSocket - SourceCode - 用C++ & CAsyncSocket 如何发送邮件 - 源码 - HackerJLY
- How to obtain access to an ActiveX control from its property page 如何从一个控件的属性页中获取对该控件的访问?
- How to delete all email in the mail server using telnet & WCScript - 如何用 Telnet 删除邮件服务器上的所有邮件
- 如何在C++中使用VBScript(使用Qt)--How to use VBScript in C++ (Using Qt)
- How to switch to another database in oracle 11g(如何在oracle中从一个数据库切换到另一个数据库)
- asp + Access 常见的数据库访问失败问题 Microsoft JET Database Engine 错误 '80004005' 解决办法
- How to get random number within a range using C++ - 如何用C++获取一定范围内的随机数
- How to add icon for the item of ListCtrl using C++ (Mail Attachment ListCtrl) - 如何用C++为ListCtrl的Item添加图标(邮件附件ListCtrl)
- How to invoke dll using C++ - C++ 如何调用DLL - 详解
- 如何访问Azure上的SharePoint 网站(How to access the SharePoint site of Azure)
- asp + Access 常见的数据库访问失败问题 Microsoft JET Database Engine 错误 '80004005' 解决办法
- How to show a modal dialog and modeless dialog using C++ - 如何用C++显示一个模态对话框和一个非模态对话框
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- Add data to the Access database using ADO
- JAVA 使用哈希表操作数据库的例子 Using Hashtables to Store & Extract results from a Database.
- CP How-To:如何使用cPanel查看站点原始访问记录(Raw Access Logs)