ado链接C++
来源:互联网 发布:2000年总决赛第场数据 编辑:程序博客网 时间:2024/06/06 08:40
#pragma warning(disable:4146)
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","ENDOFFILE")
#include <stdio.h>
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
void ConnectionStringX(_ConnectionPtr pConnection); //连接数据库
_bstr_t GetState(int intState); //得到连接状态
void PrintProviderError(_ConnectionPtr pConnection); //输出错误
void PrintComError(_com_error &e); //输出错误
void ExitConnect(_ConnectionPtr pConnection); //关闭连接
void main()
{
// 初始化OLE/COM库环境
if (FAILED(::CoInitialize(NULL)))
{
return;
}
//创建连接对象
_ConnectionPtr pConnection = NULL;
ConnectionStringX(pConnection);
ExitConnect(pConnection);
// 释放环境
::CoUninitialize();
getchar();
}
void ConnectionStringX(_ConnectionPtr pConnection)
{
HRESULT hr = S_OK;
try
{
//ADO方式连接数据库
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=HP-PC";
pConnection->ConnectionTimeout = 10;
pConnection->Open("", "", "", adConnectUnspecified);
printf("Connection1 state: %s\n", (LPCSTR)GetState(pConnection->State));
}
catch (_com_error &e)
{
//打印错误
PrintProviderError(pConnection);
PrintComError(e);
}
}
//得到连接状态
_bstr_t GetState(int intState)
{
_bstr_t strState;
switch (intState)
{
case adStateClosed:
strState = "adStateClosed";
break;
case adStateOpen:
strState = "adStateOpen";
break;
default:
;
}
return strState;
}
//打印连接过程中的错误消息
void PrintProviderError(_ConnectionPtr pConnection)
{
// 打印连接对象的发生的错误
ErrorPtr pErr = NULL;
if ((pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
for (long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number, (LPCSTR)pErr->Description);
}
}
}
//打印COM错误
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR)bstrSource);
printf("\tDescription = %s\n", (LPCSTR)bstrDescription);
}
//关闭连接
void ExitConnect(_ConnectionPtr pConnection)
{
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
}
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","ENDOFFILE")
#include <stdio.h>
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
void ConnectionStringX(_ConnectionPtr pConnection); //连接数据库
_bstr_t GetState(int intState); //得到连接状态
void PrintProviderError(_ConnectionPtr pConnection); //输出错误
void PrintComError(_com_error &e); //输出错误
void ExitConnect(_ConnectionPtr pConnection); //关闭连接
void main()
{
// 初始化OLE/COM库环境
if (FAILED(::CoInitialize(NULL)))
{
return;
}
//创建连接对象
_ConnectionPtr pConnection = NULL;
ConnectionStringX(pConnection);
ExitConnect(pConnection);
// 释放环境
::CoUninitialize();
getchar();
}
void ConnectionStringX(_ConnectionPtr pConnection)
{
HRESULT hr = S_OK;
try
{
//ADO方式连接数据库
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=HP-PC";
pConnection->ConnectionTimeout = 10;
pConnection->Open("", "", "", adConnectUnspecified);
printf("Connection1 state: %s\n", (LPCSTR)GetState(pConnection->State));
}
catch (_com_error &e)
{
//打印错误
PrintProviderError(pConnection);
PrintComError(e);
}
}
//得到连接状态
_bstr_t GetState(int intState)
{
_bstr_t strState;
switch (intState)
{
case adStateClosed:
strState = "adStateClosed";
break;
case adStateOpen:
strState = "adStateOpen";
break;
default:
;
}
return strState;
}
//打印连接过程中的错误消息
void PrintProviderError(_ConnectionPtr pConnection)
{
// 打印连接对象的发生的错误
ErrorPtr pErr = NULL;
if ((pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
for (long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number, (LPCSTR)pErr->Description);
}
}
}
//打印COM错误
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR)bstrSource);
printf("\tDescription = %s\n", (LPCSTR)bstrDescription);
}
//关闭连接
void ExitConnect(_ConnectionPtr pConnection)
{
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
}
0 0
- c#-ADO.NET链接数据库
- ADO 动态链接数据库
- ADO.NET 链接数据库
- ado链接C++
- ADO 动态链接数据库
- [C#]ado.net 转 ado
- ADO.NET链接和摘要
- vc 用ADO链接数据库
- ADO链接数据库必看
- vc ado链接oracle数据库
- VC中ADO的链接
- VC ADO链接ACCESS数据库
- 求助C++ADO
- C++ADO读取数据库
- C#:ADO.NET简介
- ADO编程详解(C++)
- C#---ADO.net
- VB中ADO控件和ADO对象的使用【链接】
- 数据挖掘的EM算法
- java基础篇——异常处理简述
- ZOJ 3203 Light Bulb (三分+计算几何)
- Android应用之——仿美团loading加载中动画
- navicat for mysql 9.0.15注册码
- ado链接C++
- Spark(十一) -- Mllib API编程 线性回归、KMeans、协同过滤演示
- linux下字符设备驱动的三种创建方式
- Autolayout(自动布局)
- 生成32位随机字符
- POJ 3744 水概率dp+水矩阵快速幂
- short类型复合操作符问题
- Android十六进制颜色说明
- PowerPC平台 Linux移植二