本文主要是介绍D-ExecuteSQL,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按道理一步步来的话是 先创建数据源,然后是session,然后是command,然后是Rowset。先CoCreateInstance()创建指向IDBpromptInitialize 的指针 pIDBpromtInitialize.
pIDBPromptInitialize调用函数 promptDatasoure创建基于对话框的未初始化的数据源 IDBInitialize pDBConnet
然后初始化 pDBConnet->Initialize()pDBConnet->QueryInterface()创建 IDBCreateSession pIDBCreateSession
pIDBCreateSession->CreateSession 创建 IOpenRowSet pIOpenRowSet上面的最终得到pIOpenRowSet。我们把上面的封装到一个函数里面BOOL CeateDBSession(IOpenRowSet* &PIDOpenRowSet);pIOpenRowSet->QueryInterface创建 IDBCreateCommand pIDCreateCommand
PIDCreateCommand->CreateCommand 创建 ICommand pICommand
pICommand->QueryInterface() 创建 ICommandText pICommandText
pICommandText->setCommandText(DBGUID_DEFAULT,PSQL);//PSQL是sql语句(字符串)pICommandText->Execute( null,IID_IRowSet,null,null,(Iunknown**)&pIRowSet);
具体代码:
#include <tchar.h>
#include <windows.h>
#include <strsafe.h>#define COM_NO_WINDOWS_H //如果已经包含了Windows.h或不使用其他Windows库函数时
#define DBINITCONSTANTS
#define INITGUID
#define OLEDBVER 0x0260 //为了ICommandStream接口定义为2.6版
#include <oledb.h>
#include <oledberr.h>
#include <msdasc.h>#define GRS_ALLOC(sz) HeapAlloc(GetProcessHeap(),0,sz)
#define GRS_CALLOC(sz) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sz)
#define GRS_SAFEFREE(p) if(NULL != p){HeapFree(GetProcessHeap(),0,p);p=NULL;}#define GRS_USEPRINTF() TCHAR pBuf[1024] = {}
#define GRS_PRINTF(...) \StringCchPrintf(pBuf,1024,__VA_ARGS__);\WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),pBuf,lstrlen(pBuf),NULL,NULL);#define GRS_SAFERELEASE(I) if(NULL != I){I->Release();I=NULL;}
#define GRS_COM_CHECK(hr,...) if(FAILED(hr)){GRS_PRINTF(__VA_ARGS__);goto CLEAR_UP;}#define GRS_DEF_INTERFACE(i) i* p##i = NULL;
#define GRS_QUERYINTERFACE(i,iid) \if(FAILED(hr = i->QueryInterface(IID_##iid, (void **)&p##iid)) )\{\GRS_PRINTF(_T("不支持'%s'接口\n"),_T(#iid));\}\else\{\GRS_PRINTF(_T("支持'%s'接口\n"),_T(#iid));\}\GRS_SAFERELEASE(p##iid);BOOL CreateDBSession(IOpenRowset* &pIOpenRowset);int _tmain()
{GRS_USEPRINTF();CoInitialize(NULL);HRESULT hr=S_OK;IOpenRowset* pIOpenRowset=NULL;IDBCreateCommand* pIDBCreateCommand=NULL;ICommand* pICommand=NULL;ICommandText* pICommandText=NULL;ICommandProperties* pICommandProperties=NULL;IRowset* pIRowset=NULL;TCHAR* pSQL=_T(" insert into student values('wangwu','15')");// 设置适合自己数据库表的语句DBPROPSET ps[1]={};DBPROP prop[2]={};if (!CreateDBSession(pIOpenRowset)){_tsystem(_T("PAUSE"));return -1;}hr=pIOpenRowset->QueryInterface(IID_IDBCreateCommand,(void**)&pIDBCreateCommand);GRS_COM_CHECK(hr,_T("获取IDBCreateCommand接口失败"));hr=pIDBCreateCommand->CreateCommand(NULL,IID_ICommand,(IUnknown**)&pICommand);GRS_COM_CHECK(hr,_T("创建ICommand接口失败"));//==================未设置属性后执行语句==========================////执行一个普通的命令hr=pICommand->QueryInterface(IID_ICommandText,(void**)&pICommandText);GRS_COM_CHECK(hr,_T("获取ICommandText接口失败"));hr=pICommandText->SetCommandText(DBGUID_DEFAULT,pSQL);GRS_COM_CHECK(hr,_T("设置SQL语句失败"));hr=pICommandText->Execute(NULL,IID_IRowset,NULL,NULL,(IUnknown**)&pIRowset);GRS_COM_CHECK(hr,_T("执行SQL语句失败"));GRS_SAFERELEASE(pIRowset);
//==================以上代码块是未设置属性后执行语句=================////==================以下代码块是设置属性后执行语句===================////设置一些属性后,在执行命令prop[0].dwPropertyID=DBPROP_UPDATABILITY;prop[0].vValue.vt=VT_I4;//设置执行后的结果集可以进行增删改查操作prop[0].vValue.lVal=DBPROPVAL_UP_INSERT | DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_DELETE;prop[0].dwOptions=DBPROPOPTIONS_REQUIRED;prop[0].colid=DB_NULLID;ps[0].guidPropertySet=DBPROPSET_ROWSET;ps[0].cProperties=1;ps[0].rgProperties=prop;hr=pICommandText->QueryInterface(IID_ICommandProperties,(void**)&pICommandProperties);GRS_COM_CHECK(hr,_T("获取ICommandProperties接口失败"));hr=pICommandProperties->SetProperties(1,ps);//在Execute前设定属性GRS_COM_CHECK(hr,_T("设定属性失败"));hr=pICommandText->Execute(NULL,IID_IRowset,NULL,NULL,(IUnknown**)&pIRowset);GRS_COM_CHECK(hr,_T("执行SQL语句失败"));//==================以上代码块是未设置属性后执行语句=================//CLEAR_UP:GRS_SAFERELEASE(pIRowset);GRS_SAFERELEASE(pICommandProperties);GRS_SAFERELEASE(pICommand);GRS_SAFERELEASE(pICommandText);GRS_SAFERELEASE(pIOpenRowset);GRS_SAFERELEASE(pIDBCreateCommand);_tsystem(_T("PAUSSE"));CoUninitialize();return 0;;
}BOOL CreateDBSession(IOpenRowset* &pIOpenRowset)
{GRS_USEPRINTF();GRS_SAFERELEASE(pIOpenRowset);IDBPromptInitialize* pIDBPromtInitialize=NULL;IDBInitialize* pDBConnet=NULL;IDBCreateSession* pIDBCreateSession=NULL;HWND hWndParent=GetDesktopWindow();CLSID clsid={};//使用oledb 数据库连接对话框链接到数据源HRESULT hr=CoCreateInstance(CLSID_DataLinks,NULL,CLSCTX_INPROC_SERVER,IID_IDBPromptInitialize,(void**)&pIDBPromtInitialize);if (FAILED(hr)){GRS_PRINTF(_T("无法创建IDBPromptInitialize接口"),hr);GRS_SAFERELEASE(pDBConnet);GRS_SAFERELEASE(pIDBPromtInitialize);return FALSE;}//下面的将弹出数据库连接对话框hr=pIDBPromtInitialize->PromptDataSource(NULL,hWndParent,DBPROMPTOPTIONS_PROPERTYSHEET,0,NULL,NULL,IID_IDBInitialize,(IUnknown**)&pDBConnet);if (FAILED(hr)){GRS_PRINTF(_T("无法创建IDBPromptInitialize接口"),hr);GRS_SAFERELEASE(pDBConnet);GRS_SAFERELEASE(pIDBPromtInitialize);return FALSE;}GRS_SAFERELEASE(pIDBPromtInitialize);hr=pDBConnet->Initialize();//根据对话框采集到的参数链接到指定的数据库if (FAILED(hr)){GRS_PRINTF(_T("初始化数据源连接错误"));GRS_SAFERELEASE(pDBConnet);return FALSE;}if (FAILED(hr=pDBConnet->QueryInterface(IID_IDBCreateSession,(void**)&pIDBCreateSession))){GRS_PRINTF(_T("获取IDBCreateSession接口失败"));GRS_SAFERELEASE(pIDBCreateSession);GRS_SAFERELEASE(pDBConnet);return FALSE;}GRS_SAFERELEASE(pDBConnet);hr=pIDBCreateSession->CreateSession(NULL,IID_IOpenRowset,(IUnknown**)&pIOpenRowset);if (FAILED(hr)){GRS_PRINTF(_T("创建Session对象获取IOpenRowset接口失败"));GRS_SAFERELEASE(pIDBCreateSession);GRS_SAFERELEASE(pIOpenRowset);return FALSE;}GRS_SAFERELEASE(pIDBCreateSession);GRS_SAFERELEASE(pDBConnet);return TRUE;
}
这篇关于D-ExecuteSQL的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!