#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();
}