ado技術目前已經成為連接數據庫的主流技術,下面我來介紹如何使用ado來動態創建Access數據庫。為了使用ado,必須引入微軟的兩個動態連接庫msadox.dll和msado15.dll:
#pragma warning (disable: 4146)
#import "c:Program FilesCommon Filessystemadomsadox.dll"
#import "c:Program FilesCommon Filessystemadomsado15.dll" no_namespace rename("EOF", "EndOfFile")
#pragma warning (default: 4146)
將上述代碼加入到stdafx.h文件中,由於ado是com組件,因此使用ado之前還要初始化com環境:
CoInitialize(NULL);
下面是一個在Access數據庫中創建表的sql語句的例子:
create table test
(
nametext(20) WITH COMPRESSION not null,
scoredecimal(12,4) not null default 0,
ID smallint not null default 0,
birthday date,
sex char(1),
CONSTRAINT CK_CH_SEX Check(SEX in ('N','V')),
CONSTRAINT PK_ID Primary Key (ID)
);
使用ADOX::CatalogPtr來創建mdb文件:
HRESULT hr = S_OK;
//Set ActiveConnection of Catalog to this string
CString strcnn(_T("Provider=Microsoft.JET.OLEDB.4.0;Data source = d: est.mdb"));
try
{
ADOX::_CatalogPtr m_pCatalog = NULL;
hr = m_pCatalog.CreateInstance(__uuidof (ADOX::Catalog));
if(FAILED(hr))
{
_com_issue_error(hr);
}
else
{
m_pCatalog->Create(_bstr_t(strcnn)); //Create MDB
}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
AfxMessageBox(_T(“error“));
}
創建一個連接對象打開剛剛建立的mdb文件:
_ConnectionPtr g_pConn;
g_pConn.CreateInstance(__uuidof(Connection));
g_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d: est.mdb;", "", "", adConnectUnspecifIEd);
下面函數用來執行sql文件中的sql語句:
// Function name : RunSqlScript
// Description: 執行SQL腳本, [email protected], 2003-09-15
// Return type: BOOL 成功返回TRUE
// Argument : LPCSTR SqlScriptFile SQL腳本文件名
// Argument : char separator SQL 分割符號, 缺省';'
// Argument : char RemarkSQL 注釋符號, 缺省'-'
BOOL RunSqlScript(LPCSTR SqlScriptFile, char separator = ';', char Remark = '-')
{
BOOL bRet = FALSE;
CFileFind finder;
CString ErrLong;
if (finder.FindFile(SqlScriptFile) == TRUE)
{
CFile fSql;
TCHAR *buffer, *pSQL, *p;
fSql.Open(SqlScriptFile,Cfile&::modeRead);
UINT nFileLength = fSql.GetLength();
buffer = (TCHAR *)malloc((nFileLength + 1) * sizeof(TCHAR));
_tcsnset(buffer, TCHAR(''), nFileLength + 1);
UINT nBytesRead = fSql.Read(buffer,nFileLength); //把sql文件內容讀入內存緩沖區
fSql.Close();
p = pSQL = buffer;
BOOL bRunOK = FALSE;
BOOL bInNote = FALSE;
BOOL bSkip = FALSE;
CString strSQL;
TCHAR ch;
ErrLog = _T("");
while (p < (buffer + nFileLength)){
//判斷是否是注釋行
if (bInNote)
{
if (*p == TCHAR('x0a'))
bInNote = FALSE;
}
else
{
if ((*p == Remark) && (*(p+1) == Remark))
{
bInNote = TRUE;
p++;
}
else
{
//判斷是否是sql語句結束標志
if (*p == separator){
strSQL = _T("");
bSkip = FALSE;
while (pSQL < p)
{
if (bSkip == FALSE){
if ((*(pSQL) == Remark) &&(*(pSQL+1) == Remark))
{
bSkip = TRUE;
pSQL++;
}
else
{
ch = *pSQL;
strSQL = strSQL+ch;
}
}
else{
if (*pSQL = TCHAR('x0a')){
bSkip = FALSE;
ch = TCHAR(' ');
strSQL = strSQL + ch;
}
}
pSQL++;
}
pSQL = p + 1;
ErrLog = ErrLog + _T("-- SQL Running ... ");
ErrLog = ErrLog + strSQL;
_variant_t vRecords;
m_nRecordsAffected = 0;
try
{
g_pConn->CursorLocation = adUseClIEnt;
g_pConn->Execute(_bstr_t((LPCTSTR)strSQL), &vRecords, adExecuteNoRecords);
m_nRecordsAffected = vRecords.iVal;
bRunOK = TRUE;
}
catch(_com_error &e)
{
bRunOK = FALSE;
}
if (bRunOK)
ErrLog = ErrLog + _T(" -- Successed! ");
else
{
ErrLog = ErrLog + _T(" -- Failed! ");
break;
}
}
}
}
p++;
}
free(buffer);
bRet = bRunOK;
}
return bRet;
}
調用RunSqlScript創建Access數據庫中的表:
RunSqlScript(_T("d:\test.sql"));
關閉數據庫連接:
g_pConn->Close();
::CoUninitialize();
以上代碼演示了怎樣在vc中利用ado根據sql文件動態創建一個Access數據庫,歡迎指正。