SOCPP.CPP
资源名称:MSDN_VC98.zip [点击查看]
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:2k
源码类别:
Windows编程
开发平台:
Visual C++
- #define UNICODE
- #define _UNICODE
- #include <windows.h>
- #include <initguid.h>
- #include <tchar.h>
- #include <sqloleid.h>
- #include <sqlole.h>
- int main (void)
- {
- HRESULT hr;
- if FAILED(hr = CoInitialize (NULL))
- {
- _tprintf(TEXT("CoInitialize Failedn"));
- return (0);
- }
- LPSQLOLESERVER pSQLServer = NULL;
- if FAILED(hr = CoCreateInstance(CLSID_SQLOLEServer, NULL, CLSCTX_INPROC_SERVER,
- IID_ISQLOLEServer, (LPVOID*)&pSQLServer))
- {
- _tprintf(TEXT("CoCreateInstance Failedn"));
- return (0);
- }
- pSQLServer->SetLoginTimeout(10);
- if FAILED(hr = pSQLServer->Connect(TEXT(""),TEXT("sa"),TEXT("")))
- {
- _tprintf(TEXT("Connect failedn"));
- LPSUPPORTERRORINFO pSEI = NULL;
- if SUCCEEDED(pSQLServer->QueryInterface(IID_ISupportErrorInfo, (LPVOID*)&pSEI))
- {
- if SUCCEEDED(pSEI->InterfaceSupportsErrorInfo(IID_ISQLOLEServer))
- {
- LPERRORINFO pEI = NULL;
- GetErrorInfo (0, &pEI);
- SQLOLE_BSTR strDescription, strSource;
- pEI->GetDescription (&strDescription);
- pEI->GetSource (&strSource);
- _tprintf(TEXT("%s returned error %d: %sn"),
- strSource,
- SCODE_CODE(hr),
- strDescription);
- pEI->Release ();
- }
- }
- else
- {
- _tprintf(TEXT("Error %dn"), HRESULT_CODE(hr));
- }
- pSEI->Release ();
- }
- else
- {
- SQLOLE_BSTR str;
- pSQLServer->GetVersionString(&str);
- _tprintf(TEXT("%sn"),str);
- LPSQLOLEAPPLICATION pApp = NULL;
- pSQLServer->GetApplication(&pApp);
- pApp->GetName(&str);
- _tprintf(TEXT("%sn"),str);
- SysFreeString((BSTR)str);
- LONG ver;
- pSQLServer->GetVersionMajor(&ver);
- _tprintf(TEXT("%dn"),ver);
- }
- pSQLServer->Release ();
- CoUninitialize ();
- return (0);
- }