c++从数据库的表中读取数据
来源:互联网 发布:淘宝网店培训多少钱 编辑:程序博客网 时间:2024/06/05 01:01
// 0518A.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
//步骤1:添加对ADO的支持
#import "C:\Program Files (x86)\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL); //初始化COM环境
_ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象
_RecordsetPtr pRst(__uuidof(Recordset));//定义记录集对象并实例化对象
try
{
//步骤2:创建数据源连接
/*打开数据库“SQLServer”,这里需要根据自己PC的数据库的情况 */
pMyConnect->Open("Provider=SQLOLEDB; Server=.;Database=B; uid=sa; pwd=123;","","",adModeUnknown);
}
catch(_com_error &e)
{
cout<<"Initiate failed!"<<endl;
cout<<e.Description()<<endl;
//cout<<e.HelpFile()<<endl;
_getch();
return 0;
}
cout<<"Connect succeed!"<<endl;
//步骤3:对数据源中的数据库/表进行操作
try
{
pRst = pMyConnect->Execute("select * from Table_BB",NULL,adCmdText);
//执行SQL: select * from Table_BB
if(!pRst->BOF)
{
pRst->MoveFirst();
}
else
{
cout<<"Data is empty!"<<endl;
return 0;
}
vector<_bstr_t> column_name;
/*存储表的所有列名,显示表的列名*/
for(int i=0; i< pRst->Fields->GetCount();i++)
{
cout<<pRst->Fields->GetItem(_variant_t((long)i))->Name<<" ";
column_name.push_back(pRst->Fields->GetItem(_variant_t((long)i))->Name);
}
cout<<endl;
/*对表进行遍历访问,显示表中每一行的内容*/
while(!pRst->adoEOF)
{
vector<_bstr_t>::iterator iter=column_name.begin();
for(iter;iter!=column_name.end();iter++)
{
if(pRst->GetCollect(*iter).vt !=VT_NULL)
{
cout<<(_bstr_t)pRst->GetCollect(*iter)<<" ";
}
else
{
cout<<"NULL"<<endl;
}
}
pRst->MoveNext();
cout<<endl;
}
}
catch(_com_error &e)
{
cout<<e.Description()<<endl;
//cout<<e.HelpFile()<<endl;
return 0;
}
_getch();
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
//步骤1:添加对ADO的支持
#import "C:\Program Files (x86)\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL); //初始化COM环境
_ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象
_RecordsetPtr pRst(__uuidof(Recordset));//定义记录集对象并实例化对象
try
{
//步骤2:创建数据源连接
/*打开数据库“SQLServer”,这里需要根据自己PC的数据库的情况 */
pMyConnect->Open("Provider=SQLOLEDB; Server=.;Database=B; uid=sa; pwd=123;","","",adModeUnknown);
}
catch(_com_error &e)
{
cout<<"Initiate failed!"<<endl;
cout<<e.Description()<<endl;
//cout<<e.HelpFile()<<endl;
_getch();
return 0;
}
cout<<"Connect succeed!"<<endl;
//步骤3:对数据源中的数据库/表进行操作
try
{
pRst = pMyConnect->Execute("select * from Table_BB",NULL,adCmdText);
//执行SQL: select * from Table_BB
if(!pRst->BOF)
{
pRst->MoveFirst();
}
else
{
cout<<"Data is empty!"<<endl;
return 0;
}
vector<_bstr_t> column_name;
/*存储表的所有列名,显示表的列名*/
for(int i=0; i< pRst->Fields->GetCount();i++)
{
cout<<pRst->Fields->GetItem(_variant_t((long)i))->Name<<" ";
column_name.push_back(pRst->Fields->GetItem(_variant_t((long)i))->Name);
}
cout<<endl;
/*对表进行遍历访问,显示表中每一行的内容*/
while(!pRst->adoEOF)
{
vector<_bstr_t>::iterator iter=column_name.begin();
for(iter;iter!=column_name.end();iter++)
{
if(pRst->GetCollect(*iter).vt !=VT_NULL)
{
cout<<(_bstr_t)pRst->GetCollect(*iter)<<" ";
}
else
{
cout<<"NULL"<<endl;
}
}
pRst->MoveNext();
cout<<endl;
}
}
catch(_com_error &e)
{
cout<<e.Description()<<endl;
//cout<<e.HelpFile()<<endl;
return 0;
}
_getch();
return 0;
}
0 0
- c++从数据库的表中读取数据
- [C#] 从数据库读取数据并输出
- 简单的从数据库中读取数据自动生成XML
- C#从sql数据库中读取数据的自我体会
- php使用PDO从数据库表中读取数据
- php使用PDO从数据库表中读取数据
- 从数据库中读取数据创建菜单
- 从数据库中读取XML数据
- 从数据库中读取序列化数据
- 从数据库中读取数据到Excel
- 幻灯片从数据库中读取数据
- 如何从数据库中读取数据
- HighCharts从数据库中读取数据
- C# 从数据库中读取数据(ExecuteReader)
- 用DataSet从oracle10g数据库中读取数据(C#)vs2008控制台程序
- 用DataSet从oracle10g数据库中读取数据(C#)vs2008 winform程序
- 用DataReader从Oracle10g数据库中读取数据(c#) vs2008控制台程序
- Delphi 从数据库中读取数据给TreeView赋值,然后从TreeView结点中读取数据
- QT 任意区域截图
- 最长上升子序列nlogn算法
- 几种PHP实现网页抓取的程序代码
- java GUI,贷款服务器
- Qt QToolBar
- c++从数据库的表中读取数据
- [Leetcode]Minimum Size Subarray Sum
- Linux下Ganglia集群监控安装、配置笔记
- ubuntu无法获得排他锁解决方法
- ubuntu15.04+kali linux
- opencv 帧率
- 大数据时代的地理信息科学与科研能力培养
- 更简单、更安全的查询方法
- vs2013 ODBC连接Oracle 学习记录