本文主要是介绍OLE DB备考,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
请输入大于10个字符的资源描述表对应部分,使用VC属性概念,参考wizard创建的代码制作表的类。
#pragma once

[
db_table(L " T_Event " )
]

class CT_Event

... {
public:
[db_column(1, status=m_dwEvent_IDStatus, length=m_dwEvent_IDLength)] LONG m_Event_ID;
[db_column(2, status=m_dwEvent_ContentStatus, length=m_dwEvent_ContentLength)] TCHAR m_Event_Content[255];
[db_column(3, status=m_dwEvent_ExecutorStatus, length=m_dwEvent_ExecutorLength)] TCHAR m_Event_Executor[20];
[db_column(4, status=m_dwEvent_StartTimeStatus, length=m_dwEvent_StartTimeLength)] DATE m_Event_StartTime;
[db_column(5, status=m_dwEvent_EndTimeStatus, length=m_dwEvent_EndTimeLength)] DATE m_Event_EndTime;
[db_column(6, status=m_dwEvent_PrivacyStatus, length=m_dwEvent_PrivacyLength)] LONG m_Event_Privacy;
[db_column(7, status=m_dwEvent_GroupStatus, length=m_dwEvent_GroupLength)] TCHAR m_Event_Group[20];
[db_column(8, status=m_dwEvent_CreatorStatus, length=m_dwEvent_CreatorLength)] TCHAR m_Event_Creator[20];
[db_column(9, status=m_dwEvent_CreateTimeStatus, length=m_dwEvent_CreateTimeLength)] DATE m_Event_CreateTime;
[db_column(10, status=m_dwEvent_UpdaterStatus, length=m_dwEvent_UpdaterLength)] TCHAR m_Event_Updater[20];
[db_column(11, status=m_dwEvent_UpdateTimeStatus, length=m_dwEvent_UpdateTimeLength)] DATE m_Event_UpdateTime;
[db_column(12, status=m_dwEvent_DeleteFlagStatus, length=m_dwEvent_DeleteFlagLength)] VARIANT_BOOL m_Event_DeleteFlag;
[db_column(13, status=m_dwEvent_AddressStatus, length=m_dwEvent_AddressLength)] TCHAR m_Event_Address[255];
[db_column(14, status=m_dwEvent_CommentStatus, length=m_dwEvent_CommentLength)] TCHAR m_Event_Comment[255];
[db_column(15, status=m_dwEvent_AllDayFlagStatus, length=m_dwEvent_AllDayFlagLength)] VARIANT_BOOL m_Event_AllDayFlag;
[db_column(16, status=m_dwEvent_LoopTypeStatus, length=m_dwEvent_LoopTypeLength)] LONG m_Event_LoopType;

DBSTATUS m_dwEvent_IDStatus;
DBSTATUS m_dwEvent_ContentStatus;
DBSTATUS m_dwEvent_ExecutorStatus;
DBSTATUS m_dwEvent_StartTimeStatus;
DBSTATUS m_dwEvent_EndTimeStatus;
DBSTATUS m_dwEvent_PrivacyStatus;
DBSTATUS m_dwEvent_GroupStatus;
DBSTATUS m_dwEvent_CreatorStatus;
DBSTATUS m_dwEvent_CreateTimeStatus;
DBSTATUS m_dwEvent_UpdaterStatus;
DBSTATUS m_dwEvent_UpdateTimeStatus;
DBSTATUS m_dwEvent_DeleteFlagStatus;
DBSTATUS m_dwEvent_AddressStatus;
DBSTATUS m_dwEvent_CommentStatus;
DBSTATUS m_dwEvent_AllDayFlagStatus;
DBSTATUS m_dwEvent_LoopTypeStatus;


DBLENGTH m_dwEvent_IDLength;
DBLENGTH m_dwEvent_ContentLength;
DBLENGTH m_dwEvent_ExecutorLength;
DBLENGTH m_dwEvent_StartTimeLength;
DBLENGTH m_dwEvent_EndTimeLength;
DBLENGTH m_dwEvent_PrivacyLength;
DBLENGTH m_dwEvent_GroupLength;
DBLENGTH m_dwEvent_CreatorLength;
DBLENGTH m_dwEvent_CreateTimeLength;
DBLENGTH m_dwEvent_UpdaterLength;
DBLENGTH m_dwEvent_UpdateTimeLength;
DBLENGTH m_dwEvent_DeleteFlagLength;
DBLENGTH m_dwEvent_AddressLength;
DBLENGTH m_dwEvent_CommentLength;
DBLENGTH m_dwEvent_AllDayFlagLength;
DBLENGTH m_dwEvent_LoopTypeLength;

void GetRowsetProperties(CDBPropSet* pPropSet)

...{
pPropSet->AddProperty(DBPROP_CANFETCHBACKWARDS, true, DBPROPOPTIONS_OPTIONAL);
pPropSet->AddProperty(DBPROP_CANSCROLLBACKWARDS, true, DBPROPOPTIONS_OPTIONAL);
pPropSet->AddProperty(DBPROP_IRowsetChange, true, DBPROPOPTIONS_OPTIONAL);
pPropSet->AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);
}

HRESULT OpenDataSource(LPCTSTR szDatabasePath)

...{
CDataSource _db;
HRESULT hr;
WCHAR* szPath;

#if !defined UNICODE || _UNICODE
USES_CONVERSION;
szPath = A2W(szDatabasePath);
#else
szPath = szDatabasePath;
#endif

const WCHAR strnn[] = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
unsigned int nlen = sizeof(strnn) + sizeof(szPath) + 1;
WCHAR* strdb = new WCHAR[nlen];
wcscpy(strdb, strnn);
wcscat(strdb, szPath);

hr = _db.OpenFromInitializationString(strdb);
delete strdb;
if (FAILED(hr))

...{
#ifdef _DEBUG
AtlTraceErrorRecords(hr);
#endif
return hr;
}
return m_session.Open(_db);
}

void CloseDataSource()

...{
m_session.Close();
}
operator const CSession&()

...{
return m_session;
}

CSession m_session;
} ;
MFC 外带ATL支持,这是stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif

#include < iostream >
#include < tchar.h >
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif

#include < afx.h >
#include < afxwin.h > // MFC core and standard components
#include < afxext.h > // MFC extensions
#include < afxdtctl.h > // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include < afxcmn.h > // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit

#include < atlbase.h >
#include < atldbcli.h >
// TODO: reference additional headers your program requires here

// #define CONNSTRING(X) "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" #X " "
主要的部分AccessAgent,使用这个类来操作Access数据库
#pragma once

#include < atldbcli.h >

class CAccessAgent

... {
public:
CAccessAgent();
CAccessAgent(LPCTSTR szDatabasePath);
static BOOL CreateAccessDatabase(LPCTSTR szDatabasePath);
//second parameter is SQL statement
//it is used for creating tables and views
//CCommand in the function has not Accessor, maybe needs CCommand<CDynamicAccessor> in future
static BOOL ExecuteSQL(LPCTSTR szDatabasePath, LPCTSTR lpsz);
int ExecuteSQL(LPCTSTR lpsz);

private:
CString m_errstr;
HRESULT m_hr;

//db_source and db_session
CDataSource m_conn;
CSession m_session;
} ;
#include " stdafx.h "
#include " AccessAgent.h "

CAccessAgent::CAccessAgent()

... {}

CAccessAgent:: ~ CAccessAgent()

... {
m_session.Close();
m_conn.Close();
}

CAccessAgent::CAccessAgent(LPCTSTR szDatabasePath)

... {
WCHAR* szPath;
#if !defined UNICODE || _UNICODE
USES_CONVERSION;
szPath = A2W(szDatabasePath);
#else
szPath = szDatabasePath;
#endif

const WCHAR strnn[] = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
unsigned int nlen = sizeof(strnn) + sizeof(szPath) + 1;
WCHAR* strdb = new WCHAR[nlen];
wcscpy(strdb, strnn);
wcscat(strdb, szPath);

HRESULT hr = m_conn.OpenFromInitializationString(strdb);
delete strdb;
if(SUCCEEDED(hr))

...{
hr = m_session.Open(m_conn);
if(FAILED(hr))

...{
m_hr = hr;
m_session.Close();
m_conn.Close();
}
}
else

...{
m_hr = hr;
m_conn.Close();
}
}

BOOL CAccessAgent::CreateAccessDatabase(LPCTSTR szDatabasePath)

... {
CDataSource ds;
IDBDataSourceAdmin* pIDBDataSourceAdmin = NULL;


CLSID clsid = ...{0xdee35070L,0x506b,0x11cf,

...{0xb1,0xaa,0x0,0xaa,0x0,0xb8,0xde,0x95}};
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
__uuidof(IDBInitialize), (void**)&ds.m_spInit);
if (FAILED(hr))
return FALSE;

USES_CONVERSION;

CDBPropSet rgPropertySet(DBPROPSET_DBINIT);
rgPropertySet.AddProperty(DBPROP_INIT_DATASOURCE,
T2BSTR(szDatabasePath));

hr = ds.m_spInit->QueryInterface(IID_IDBDataSourceAdmin,
(void**)&pIDBDataSourceAdmin);
if(FAILED(hr))

...{
ds.Close();
return FALSE;
}

hr = pIDBDataSourceAdmin->CreateDataSource(1,
&rgPropertySet, NULL, IID_NULL, NULL);
if(FAILED(hr))

...{
pIDBDataSourceAdmin->Release();
ds.Close();
return FALSE;
}

pIDBDataSourceAdmin->Release();

ds.Close();

return TRUE;
}
BOOL CAccessAgent::ExecuteSQL(LPCTSTR szDatabasePath, LPCTSTR lpsz)

... {
HRESULT hr;
CDataSource ds;
CSession session;
CCommand<CDynamicAccessor> comm;

WCHAR* szPath;
#if !defined UNICODE || _UNICODE
USES_CONVERSION;
szPath = A2W(szDatabasePath);
#else
szPath = szDatabasePath;
#endif

const WCHAR strnn[] = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
unsigned int nlen = sizeof(strnn) + sizeof(szPath) + 1;
WCHAR* strdb = new WCHAR[nlen];
wcscpy(strdb, strnn);
wcscat(strdb, szPath);

hr = ds.OpenFromInitializationString(strdb);
delete strdb;

if(FAILED(hr))

...{
ds.Close();
return FALSE;
}

hr = session.Open(ds);
if(FAILED(hr))

...{
session.Close();
ds.Close();
return FALSE;
}

hr = comm.Create(session, lpsz, DBGUID_DBSQL);
if(SUCCEEDED(hr))

...{
CDBPropSet propset(DBPROPSET_ROWSET);
propset.AddProperty(DBPROP_IRowsetChange, true);
propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);

hr = comm.Open(&propset, NULL, false, 0);
if(FAILED(hr))

...{
comm.Close();
session.Close();
ds.Close();
return FALSE;
}
}
else

...{
comm.Close();
session.Close();
ds.Close();
return FALSE;
}

comm.Close();
session.Close();
ds.Close();
return TRUE;
}

BOOL CAccessAgent::ExecuteSQL(LPCTSTR lpsz)

... {
BOOL ret = TRUE;
HRESULT hr;
CCommand<CDynamicAccessor> comm;

hr = comm.Create(m_session, lpsz, DBGUID_DBSQL);
if(SUCCEEDED(hr))

...{
CDBPropSet propset(DBPROPSET_ROWSET);
propset.AddProperty(DBPROP_IRowsetChange, true);
propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);

hr = comm.Open(&propset, NULL, false, 0);
if (FAILED(hr))

...{
ret = FALSE;
m_hr = hr;
}
}
else

...{
ret = FALSE;
m_hr = hr;
}
comm.Close();
return ret;
}
程序启动点
// AccessManipulator.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
#include " AccessManipulator.h "
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include " AccessAgent.h "
#include " T_Event.h "

// The one and only application object
CWinApp theApp;

using namespace std;

int _tmain( int argc, TCHAR * argv[], TCHAR * envp[])

... {
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))

...{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed "));
nRetCode = 1;
}
else

...{
HRESULT hr = S_OK;
CString dbPath = "../AccessTest.mdb";

#ifdef _DEBUG
CFileFind finder;
BOOL bWorking = finder.FindFile(dbPath.GetBuffer());
if (bWorking)

...{
::DeleteFile(dbPath.GetBuffer());
}
else
printf("delete mdb file failed! ");
#endif
//init COM Interface
hr = CoInitialize(NULL);
if(FAILED(hr))

...{
printf("COM initialzing failed! ");
}
if(!CAccessAgent::CreateAccessDatabase(dbPath.GetBuffer()))

...{
printf("create database failed! ");
}
//create table,
//do the same way, when creating your views.
CAccessAgent agent(dbPath.GetBuffer());
CString strSqlCreateEventTable = "CREATE TABLE T_Event(
Event_ID INTEGER,
Event_Content TEXT(255),
Event_Executor CHAR(20),
Event_StartTime DATETIME,
Event_EndTime DATETIME,
Event_Privacy INTEGER,
Event_Group CHAR(20),
Event_Creator CHAR(20),
Event_CreateTime DATETIME,
Event_Updater CHAR(20),
Event_UpdateTime DATETIME,
Event_DeleteFlag BIT,
Event_Address TEXT(255),
Event_Comment TEXT(255),
Event_AllDayFlag BIT,
Event_LoopType INTEGER
)";
if(!agent.ExecuteSQL(strSqlCreateEventTable.GetBuffer()))

...{
printf("create event table failed! ");
}
//example for inserting into and reading from T_Event
CT_Event event;
event.OpenDataSource(dbPath);
hr = event.OpenRowset(event);

//event.m_Event_ID = 1;
//_tcscpy(event.m_Event_Content, "qwieojaodjfoaiwejrower");
//_tcscpy(event.m_Event_Executor, "qwieojaodjfoaiwejrower");
//event.m_Event_StartTime = 12.00;
//event.m_Event_EndTime = 1.00;
//event.m_Event_Privacy = 123123;
//_tcscpy(event.m_Event_Group, "qwieojaodjfoaiwejrower");

//hr = event.Insert();
//event.MoveNext();
//hr = event.GetData();

//printf("%d, %s ", event.m_Event_ID, event.m_Event_Content);
event.CloseDataSource();
}

//Uninit COM Interface
CoUninitialize();

getchar();
return nRetCode;
}
这篇关于OLE DB备考的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!