ADODB连接数据库,通过msado15组件实现,VC2005
2012-11-30 17:20
357 查看
#include "stdafx.h"
#import "C://Program Files//Common Files//System//ado//msado15.dll" no_namespace rename("EOF", "adoEOF")
//确定你有这个DLL
void show1();
int main(int argc, char* argv[])
{
show1();
return 0;
}
void show1()
{
CoInitialize(NULL);
HRESULT hr;
try
{
//_ConnectionPtr pConn("ADODB.Connection");
_ConnectionPtr pConn;
hr = pConn.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
_RecordsetPtr pRst("ADODB.Recordset");
pConn->put_ConnectionTimeout(long(5));
//pConn->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb", "", "", adModeUnknown);
pConn->Open("Provider=sqloledb;Data Source=.;Initial Catalog=CMS;User Id=sa;Password=sa;","","",adConnectUnspecified); //SqlServer2005
//可以通过设置连接不同的数据库,具体请参考MSDN
pRst->Open("SELECT * FROM CMS",// 查询DemoTable表中所有字段
pConn.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
//pRst->Open("CMS",_variant_t((IDispatch *)pConn,true),adOpenStatic,adLockReadOnly,adCmdTable);
int i=0;
while(!pRst->adoEOF)
{
i++;
printf("%d/n",i);
//_variant_t var= pRst->GetCollect("cms_id");
//_variant_t var= pRst->GetCollect(long(0));
//if(var.vt != VT_NULL)
// printf("%s/n",(char *)(LPCSTR)_bstr_t(var));
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
pRst->MoveNext();
}
/* pRst->MoveFirst();
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
pRst->Move(3);
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
pRst->MoveLast();
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));*/
//pRst->MovePrevious();
//pRst->Delete(adAffectCurrent);
// 参数adAffectCurrent为删除当前记录
//pRst->Update();
_CommandPtr m_pCommand;
m_pCommand.CreateInstance(__uuidof(Command));
// 将库连接赋于它
m_pCommand->ActiveConnection = pConn;
// SQL语句
//m_pCommand->CommandText = "SELECT * FROM CMS";
m_pCommand->CommandText = "update CMS set type_id=2 where cms_id=1";
// 执行SQL语句,返回记录集
pRst = m_pCommand->Execute(NULL, NULL,adCmdText);
//while(!pRst->adoEOF)
//{
// i++;
// printf("%d/n",i);
// //_variant_t var= pRst->GetCollect("cms_id");
// //_variant_t var= pRst->GetCollect(long(0));
// //if(var.vt != VT_NULL)
// // printf("%s/n",(char *)(LPCSTR)_bstr_t(var));
// printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
// pRst->MoveNext();
//}
pRst->Close();
if( pConn->State )
pConn->Close();
}
}
catch(_com_error &e)
{
printf("Description= '%s'/n",(char*)e.Description());
}
::CoUninitialize();
}
#import "C://Program Files//Common Files//System//ado//msado15.dll" no_namespace rename("EOF", "adoEOF")
//确定你有这个DLL
void show1();
int main(int argc, char* argv[])
{
show1();
return 0;
}
void show1()
{
CoInitialize(NULL);
HRESULT hr;
try
{
//_ConnectionPtr pConn("ADODB.Connection");
_ConnectionPtr pConn;
hr = pConn.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
_RecordsetPtr pRst("ADODB.Recordset");
pConn->put_ConnectionTimeout(long(5));
//pConn->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb", "", "", adModeUnknown);
pConn->Open("Provider=sqloledb;Data Source=.;Initial Catalog=CMS;User Id=sa;Password=sa;","","",adConnectUnspecified); //SqlServer2005
//可以通过设置连接不同的数据库,具体请参考MSDN
pRst->Open("SELECT * FROM CMS",// 查询DemoTable表中所有字段
pConn.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
//pRst->Open("CMS",_variant_t((IDispatch *)pConn,true),adOpenStatic,adLockReadOnly,adCmdTable);
int i=0;
while(!pRst->adoEOF)
{
i++;
printf("%d/n",i);
//_variant_t var= pRst->GetCollect("cms_id");
//_variant_t var= pRst->GetCollect(long(0));
//if(var.vt != VT_NULL)
// printf("%s/n",(char *)(LPCSTR)_bstr_t(var));
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
pRst->MoveNext();
}
/* pRst->MoveFirst();
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
pRst->Move(3);
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
pRst->MoveLast();
printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));*/
//pRst->MovePrevious();
//pRst->Delete(adAffectCurrent);
// 参数adAffectCurrent为删除当前记录
//pRst->Update();
_CommandPtr m_pCommand;
m_pCommand.CreateInstance(__uuidof(Command));
// 将库连接赋于它
m_pCommand->ActiveConnection = pConn;
// SQL语句
//m_pCommand->CommandText = "SELECT * FROM CMS";
m_pCommand->CommandText = "update CMS set type_id=2 where cms_id=1";
// 执行SQL语句,返回记录集
pRst = m_pCommand->Execute(NULL, NULL,adCmdText);
//while(!pRst->adoEOF)
//{
// i++;
// printf("%d/n",i);
// //_variant_t var= pRst->GetCollect("cms_id");
// //_variant_t var= pRst->GetCollect(long(0));
// //if(var.vt != VT_NULL)
// // printf("%s/n",(char *)(LPCSTR)_bstr_t(var));
// printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
// pRst->MoveNext();
//}
pRst->Close();
if( pConn->State )
pConn->Close();
}
}
catch(_com_error &e)
{
printf("Description= '%s'/n",(char*)e.Description());
}
::CoUninitialize();
}
相关文章推荐
- ADODB连接数据库,通过msado15组件实现,VC2005
- 通过Visual Studio 2005 中的数据源选择对话框实现数据库连接配置(用户使用)
- Android通过Servlet连接MySQL 实现登陆/注册(数据库+服务器+客户端)
- plsql 通过修改配置文件的方式实现数据库的连接
- VC++ 通过ADO连接数据库查询时返回空值报错的解决方案
- VC++中使用MFC通过ADO连接数据库
- 通过动态代理(Proxy)实现的数据库连接池的创建连接与归还链接的操作的简单的实现流程
- 通过VS中的数据源选择对话框简单实现数据库连接配置[图]
- 通过代理类实现java连接数据库(使用dao层操作数据)实例分享
- [MySQL]VC++通过Pipe连接数据库(ODBC)
- VC++中使用MFC通过ADO连接数据库方法小结(不包括异常的捕捉
- MySQL通过增加用户实现远程连接数据库
- vc使用ADO连接数据库 ADODB中的RecordSet.Open打开记录的两个参数adOpenKeyset、adLockBatchOptimistic的说明:
- VC++中使用MFC通过ADO连接数据库
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端),androidmysql
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端)
- 通过动态代理(Proxy)实现的数据库连接池的创建连接与归还链接的操作的简单的实现流程
- Matlab R2012a 通过JDBC方式连接Sqlserver 2005数据库
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端)
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端),android+mysql