哎呀媽呀!小伙伴們有沒有想過要用C語言去鏈接上數(shù)據(jù)庫呢???嘿嘿嘿,沒錯,就是OleDbConnection啦,那么今天蒟蒻我就來給大家整整這個操作吧!
首先,我們得知道什么是OleDbConnection?它可是一個能讓我們用C語言去鏈接各種數(shù)據(jù)庫的好東西嘞!它實現(xiàn)了OLE DB協(xié)議的一組類,允許以 OleDbCommand、OleDbDataAdaptor 和 OleDbDataReader 等方式使用 OLE DB 訪問數(shù)據(jù)庫,妙不可言吧!
好的,既然我們要使用OleDbConnection進行數(shù)據(jù)庫鏈接,那么我們得先開動手指打一下代碼了。(下面的代碼是針對Microsoft Access數(shù)據(jù)庫的,其他數(shù)據(jù)庫需要修改ConnectionString參數(shù))
首先,我們需要引入相應的頭文件:
#include #include #include #include #include #include #include 接下來,需要定義一些宏和變量,詳細的意思注釋如下: #define BUFFER_LEN 64 #define TABLENAME _T("PersonInfo") //數(shù)據(jù)庫中的表名 #define STMTLEN 100 TCHAR szProvider[] = _T("Microsoft.Jet.OLEDB.4.0"); TCHAR szDataSource[] = _T("D:\\Data\\students.mdb"); //數(shù)據(jù)庫文件路徑 TCHAR szPersist[] = _T("Persist Security Info=False"); TCHAR szConnectString[BUFFER_LEN]; DBPROPIDSET rgPropSet[1]; DWORD dwStatus; HRESULT hr; IDBInitialize *pIDBInitialize = NULL; IOpenRowset *pIOpenRowset = NULL; IDBCreateSession *pIDBCreateSession; ISessionProperties *pISessionProperties = NULL; DBID dbidTable; DBID dbidCol; HACCESSOR hAccessor; 這里面涉及了一些重要的變量和宏的定義,要認真看喲! 然后,我們需要定義一個函數(shù),這個函數(shù)是用來建立連接的,代碼如下: HRESULT EstablishConnection() { HRESULT hr; TCHAR strFilePath[MAX_PATH] = {0}; // 創(chuàng)建工作空間 hr = CoCreateInstance(CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (LPVOID *)&pIDBInitialize); if (FAILED(hr)) { printf("Create workspace failed!\n"); return hr; } // 設置數(shù)據(jù)源的屬性 hr = pIDBInitialize -> Initialize(); if(FAILED(hr)) { printf("Initialize() has failed!\n"); return hr; } // 獲取IDBCreateSession接口 hr = pIDBInitialize -> QueryInterface( IID_IDBCreateSession, (void**)&pIDBCreateSession ); if (FAILED(hr)) { printf("Query interface of IDBCreateSession fialed!\n"); return hr; } //建立連接的字符串 _stprintf_s(szConnectString, BUFFER_LEN, _T("Provider=%s;Data Source=%s;Jet OLEDB:Database Password=; %s"),szProvider, szDataSource, szPersist); hr = pIDBCreateSession -> CreateSession(NULL, IID_ISessionProperties, (IUnknown **)&pISessionProperties); if (FAILED(hr)) { printf("CreateSession() failed!\n"); return hr; } //設置數(shù)據(jù)源屬性 hr = pISessionProperties -> SetProperty(DBPROPSET_DBINIT, DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, VARIANT_TRUE); if (FAILED(hr)) { printf("Set DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO is failed!\n"); return hr; } hr = pISessionProperties -> SetProperty(DBPROPSET_DBINIT, DBPROP_INIT_PROMPT, (int)FALSE); if (FAILED(hr)) { printf("Set DBPROP_INIT_PROMPT is failed!\n"); return hr; } hr = pISessionProperties -> SetProperty(DBPROPSET_DBINIT, DBPROP_INIT_DATASOURCE, szDataSource); if (FAILED(hr)) { printf("Set DBPROP_INIT_DATASOURCE is failed!\n"); return hr; } hr = pISessionProperties -> SetProperty(DBPROPSET_DBINIT, DBPROP_INIT_PROVIDERSTRING, szConnectString); if (FAILED(hr)) { printf("Set DBPROP_INIT_PROVIDERSTRING is failed!\n"); return hr; } return hr; } 這個函數(shù)是我們建立連接的核心所在,在調用此函數(shù)之前,需要先設置一些參數(shù)以及數(shù)據(jù)源的屬性等。 最后,我們再寫一個函數(shù),來釋放資源,代碼如下: void ReleaseResource() { if (hAccessor) { pIOpenRowset ->ReleaseAccessor(hAccessor, NULL); hAccessor = NULL; } if (pIOpenRowset) { pIOpenRowset -> Release(); pIOpenRowset = NULL; } if (pIDBCreateSession) { pIDBCreateSession -> Release(); pIDBCreateSession = NULL; } if (pIDBInitialize) { pIDBInitialize -> Uninitialize(); pIDBInitialize -> Release(); pIDBInitialize = NULL; } if (pISessionProperties) { pISessionProperties -> Release(); pISessionProperties = NULL; } } 那么,我們現(xiàn)在就可以愉快地使用OleDbConnection 來鏈接上我們的數(shù)據(jù)庫啦!下面給大家一個簡單的查詢的例子作為參考: int main(void) { HRESULT hr; IRowset *pIRowset = NULL; HROW hRow; HACCESSOR hAccessor; ULONG ulTableType = 0; DBBINDING dbBind[3]; BYTE buffer[1024] = {0}; TCHAR stmt[100] = _T("Select * from PersonInfo"); TCHAR tchName[100] = { 0 }, tchSex[100] = { 0 }, tchAge[5] = { 0 }; TCHAR strDataSource[MAX_PATH] = { 0 }; //建立連接 hr = EstablishConnection(); if (FAILED(hr)) { printf("EstablishConnection() error!\n"); return -1; } //獲取IOpenRowset指針 hr = pIDBCreateSession -> GetDataSource(NULL, IID_IOpenRowset, (IUnknown **)&pIOpenRowset); if (FAILED(hr)) { printf("GetDataSource() failed!\n"); return -1; } //執(zhí)行查詢 hr = pIOpenRowset -> OpenRowset(NULL, &dbidTable, NULL, IID_IRowset, 0, NULL, (IUnknown **)&pIRowset); if (FAILED(hr)) { printf("OpenRowset() failed!\n"); return -1; } //為檢索結果創(chuàng)建訪問方法 hr = pIRowset -> GetNextRows(0, 0, 1, &ulTableType, &hRow); if (FAILED(hr)) { printf("GetNextRows() failed!\n"); return -1; } //設置訪問器 dbBind[0].iOrdinal = 1; dbBind[0].wType = DBTYPE_WSTR; dbBind[0].pTypeInfo= NULL; dbBind[0].pBindExt = NULL; dbBind[0].pObject = NULL; dbBind[0].dwPart = DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS; dbBind[0].pVarData = NULL; dbBind[0].obLength = 0; dbBind[0].obStatus = 0; dbBind[0].cbMaxLen = sizeof(tchName); dbBind[0].obValue = 0; dbBind[1].iOrdinal = 2; dbBind[1].wType = DBTYPE_WSTR; dbBind[1].pTypeInfo= NULL; dbBind[1].pBindExt = NULL; dbBind[1].pObject = NULL; dbBind[1].dwPart = DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS; dbBind[1].pVarData = NULL; dbBind[1].obLength = sizeof(tchName); dbBind[1].obStatus = dbBind[1].obLength + dbBind[1].cbMaxLen; dbBind[1].cbMaxLen = sizeof(tchSex); dbBind[1].obValue = dbBind[1].obStatus + dbBind[1].cbMaxLen; dbBind[2].iOrdinal = 3; dbBind[2].wType = DBTYPE_WSTR; dbBind[2].pTypeInfo= NULL; dbBind[2].pBindExt = NULL; dbBind[2].pObject = NULL; dbBind[2].dwPart = DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS; dbBind[2].pVarData = NULL; dbBind[2].obLength = sizeof(tchName) + sizeof(tchSex); dbBind[2].obStatus = dbBind[2].obLength + dbBind[2].cbMaxLen; dbBind[2].cbMaxLen = sizeof(tchAge); dbBind[2].obValue = dbBind[2].obStatus + dbBind[2].cbMaxLen; hr = pIRowset -> CreateAccessor(DBACCESSOR_ROWDATA, sizeof(dbBind)/sizeof(dbBind[0]), dbBind, 0, &hAccessor, NULL); if (FAILED(hr)) { printf("CreateAccessor() failed!\n"); return -1; } //檢索數(shù)據(jù) while(hr != DB_S_ENDOFROWSET) { hr = pIRowset -> GetData(hRow, hAccessor, buffer); if (SUCCEEDED(hr)) { tmemcpy(tchName, (TCHAR *)(buffer + dbBind[0].obValue), dbBind[0].cbMaxLen); tmemcpy(tchSex , (TCHAR *)(buffer + dbBind[1].obValue), dbBind[1].cbMaxLen); tmemcpy(tchAge , (TCHAR *)(buffer + dbBind[2].obValue), dbBind[2].cbMaxLen); printf("%s\t%s\t%s\n", tchName, tchSex, tchAge); } } //釋放資源 ReleaseResource(); system("pause"); return 0; } 好啦,上面就是關于使用C語言連接OleDbConnection數(shù)據(jù)庫的全部操作步驟啦!在實際使用中,還是要多花時間多練習,熟能生巧嘛! yinyiprinting.cn 寧波海美seo網(wǎng)絡優(yōu)化公司 是網(wǎng)頁設計制作,網(wǎng)站優(yōu)化,企業(yè)關鍵詞排名,網(wǎng)絡營銷知識和開發(fā)愛好者的一站式目的地,提供豐富的信息、資源和工具來幫助用戶創(chuàng)建令人驚嘆的實用網(wǎng)站。 該平臺致力于提供實用、相關和最新的內容,這使其成為初學者和經驗豐富的專業(yè)人士的寶貴資源。
聲明本文內容來自網(wǎng)絡,若涉及侵權,請聯(lián)系我們刪除! 投稿需知:請以word形式發(fā)送至郵箱[email protected]
博主的分享的東西真的很是在有實用,可以學要很多的東西需要慢慢的吸收