前言:
鄙司原始用的都是ADO来访问数据库,而我现在着手的项目是从我的GPS历史数据库中,取出历时数据的一个接口,一个DLL.用ADO写完之后,测试下来,平均4000条的数据,需要 180 毫秒左右. 包括数据包的排序.领导说有点慢.他是用C#写的.
而后我查找了资料,决定用Native Client OLEDB来试一试. 测试之后,效果还不错.取4000条的数据,只花了50毫秒左右,足足快了三倍.当然这也包括了排序.
鉴于此外加OLEDB资料很少,把我的代码分享出来.
WO 对 OLEDB 进行了简单的封装. 以下是项目的OLEDB部分代码.
//NativeClientOLEDB.h
1 #pragma once 2 3 #include <oledb.h> 4 #include <oledberr.h> 5 #include <stdio.h> 6 #include <stddef.h> // for offsetof 7 8 #include <msdaguid.h> 9 #include <msdasql.h> 10 #include <msdasc.h> 11 #include <sqlncli.h> 12 #include <string> 13 //#include "C:\Program Files\Microsoft SQL Server\110\SDK\Include\sqlncli.h" 14 15 #ifndef _AUTHOR_NAME 16 #define _AUTHOR_NAME "沈春钟" 17 #define _CREATE_DATE "2016-05-18" 18 #define _LIBRARY_TITLE "NativeClientOLEDB" 19 #define _LIBRARY_DESC "对NaTiveClientOLEDB的封装" 20 #define _MAINTAIN_CONTACT "SamRichard@live.cn" 21 #define _MAX_COL_NUM 100 22 #endif 23 24 #pragma comment(lib, "sqlncli11.lib") 25 26 27 // @type UWORD | 2 byte unsigned integer. 28 typedef unsigned short UWORD; 29 30 // @type SDWORD | 4 byte signed integer. 31 typedef signed long SDWORD; 32 33 using namespace std; 34 35 //委托表数据结构体 36 #pragma pack(push, 1) 37 class Data 38 { 39 //SDWORD SOID_len; // Length of data (not space allocated). 40 //DWORD SOID_status; // Status of column. 41 //int SOID_value; 42 }; 43 #pragma pack(pop) 44 45 //// How to lay out each column in memory. 46 //struct COLUMNDATA { 47 // SDWORD idLen; // Length of data (not space allocated). 48 // DWORD idStatus; // Status of column. 49 // int id; // Store data here as a variant. 50 // SDWORD dateLen; 51 // DWORD dateStatus; 52 // char date[21]; 53 //}; 54 55 class CNativeClientOLEDB 56 { 57 public: 58 CNativeClientOLEDB(void); 59 ~CNativeClientOLEDB(void); 60 61 public: 62 //初始化连接 63 HRESULT Init(CStringW strDataSource, CStringW strUserID, CStringW strPassWD, CStringW strCataLOG); 64 //不建议用此方式初始化 65 HRESULT Init(CStringW strConnectionString); 66 //释放连接 67 void UnInit(); 68 69 protected: 70 //函数申明 71 virtual void set_bindings(); 72 //设置每列数据列的类型 73 virtual void SetColType(void); 74 //设置每列数据列的长度 75 virtual void SetColLen(void); 76 77 //bind one by one 78 void set_bind(DBBINDING &binding, int col, DBBYTEOFFSET len_offset, DBBYTEOFFSET status_offset, DBBYTEOFFSET value_offset, DBLENGTH len, DBTYPE type); 79 80 void DumpErrorInfo(IUnknown* pObjectWithError, REFIID IID_InterfaceWithError); 81 82 //HRESULT FastInsertData(); 83 //快速查询数据 84 HRESULT FastQueryData(CStringW strSql,ULONG& nColNum, IRowset** pIRowset,IAccessor** pIAccessor, HACCESSOR& hAccessor); 85 //释放RowSet和IAccessor 86 HRESULT ReleaseRowsetAndIAccessor(IRowset** pIRowset,IAccessor** pIAccessor,const HACCESSOR& hAccessor); 87 //快速插入数据 88 HRESULT FastInsertData(CStringW strTable, ULONG& nColNum, IRowsetFastLoad** pIRowsetFastLoad, IAccessor** pIAccessor, HACCESSOR& hAccessor); 89 //释放RowsetFast和IAccessor 90 HRESULT ReleaseRowsetFastLoadAndIAccessor(IRowsetFastLoad** pIRowset,IAccessor** pIAccessor,const HACCESSOR& hAccessor); 91 92 private: 93 94 // Given an ICommand pointer, properties, and query, a rowsetpointer is returned. 95 HRESULT DBInitAndConnect(DBPROPSET* rgPropertySets, ULONG ulcPropCount, CLSID clsidProv); 96 97 // Use to set properties and execute a given query. 98 HRESULT ExecuteQuery(IDBCreateCommand* pIDBCreateCommand, 99 WCHAR* pwszQuery, 100 DBPROPSET* rgPropertySets, 101 ULONG ulcPropCount, 102 LONG* pcRowsAffected, 103 IRowset** ppIRowset, 104 BOOL fSuccessOnly = TRUE); 105 106 // Use to set up options for call to IDBInitialize::Initialize. 107 void SetupOption(DBPROPID PropID, WCHAR *wszVal, DBPROP * pDBProp); 108 109 // Sets fastload property on/off for session. 110 HRESULT SetFastLoadProperty(BOOL fSet); 111 112 public: 113 //创建会话和命令 114 HRESULT CreateSessionAndCommand(); 115 //释放会话和命令 116 HRESULT ReleaseSessionAndCommand(); 117 //设置模式和创建会话 118 HRESULT SetPropertiesAndCreateSessionAndIOpenRowset(BOOL bFast = TRUE); 119 //释放会话 120 HRESULT ReleaseSessionIOpenRowset(); 121 122 private: 123 //初始化层 124 IMalloc* m_pIMalloc; 125 IDataInitialize* m_pIDataInitialize; 126 IDBInitialize* m_pIDBInitialize; 127 // OLE initialized? 128 BOOL m_bInitialized; 129 //回话和命令层 130 IDBCreateSession* m_pIDBSession; 131 IOpenRowset* m_pIOpenRowset; 132 ICommand* m_pICommand; 133 IDBCreateCommand* m_pIDBCreateCommand; 134 ICommandText* m_pICommandText; 135 DBID TableID; 136 137 protected: 138 //binding数组 139 DBBINDING m_bindings[_MAX_COL_NUM]; 140 141 //委托表每列的数据类型 142 DBTYPEENUM col_type[_MAX_COL_NUM]; 143 144 //委托表每列的数据长度 145 DBBYTEOFFSET col_len[_MAX_COL_NUM]; 146 }; 147 148