最近偶然要做一个小型的数据库管理系统,为了方便用户无需自己用access创建数据库,所以,我必须设法让程序能够创建数据库,数据库表以及判断数据库表的存在。下面的程序代码希望能够为遇到这方面困难的朋友提供帮助:(2008/11/5重新编辑)
- //1.引入ado库
- #import "c:/Program Files/Common Files/System/ADO/Msadox.dll" rename_namespace("ADOCG") rename("EOF", "adoXEOF") rename("DataTypeEnum","adoXDataTypeEnum")
- #import "C:/Program Files/Common Files/System/ADO/msado15.dll" named_guids rename("EOF","adoEOF"), rename("BOF","adoBOF")
- using namespace ADODB;
- using namespace ADOCG;
- #define TESTHR(x) if FAILED(x) _com_issue_error(x);
- //2.创建ACCESS数据库
- BOOL CDeRenDlg::CreateMdb(CString strDBName)
- {
- CString strMdbConn = "Provider='Microsoft.JET.OLEDB.4.0';Data source = " + strDBName;
- try
- {
- HRESULT hr = S_OK;
- _CatalogPtr pCatalog = NULL;
- _bstr_t cnnstring(strMdbConn);
-
- TESTHR(hr = pCatalog.CreateInstance(__uuidof (Catalog)));
- pCatalog->Create(cnnstring);
- }
- catch(_com_error e)
- {
- _bstr_t bstrDescription(e.Description());
- CString strErro=CString(_T("创建ACCEESS数据库出错: "))
- + (LPCSTR)e.Description()
- + CString(_T("Create ACCESS DB error: "))
- + (LPCSTR)e.Description();
- AfxMessageBox(strErro);
- return FALSE;
- }
- return TRUE;
- }
- //3.创建数据库表-材料表
- BOOL CDeRenDlg::CreateTable( CString strTabName)
- {
- HRESULT hr = S_OK;
- _CatalogPtr pCatalog = NULL;
- _TablePtr pTableNew = NULL;
- _IndexPtr pIndexNew = NULL;
- _IndexPtr pIndex = NULL;
- _ColumnPtr pColumn = NULL;
- CString strConn,strDBName;
- strDBName=this->GetDBName(m_nDBYear);
- CFileFind ff;
- if(!ff.FindFile(strDBName))
- {
- AfxMessageBox("数据库不存在,请先在高级设置对话框中创建数据库!");
- return FALSE;
- }
- strConn="Provider='Microsoft.JET.OLEDB.4.0';Data source = " + strDBName;
- _bstr_t strcnn(strConn);
- try
- {
- TESTHR(hr = pCatalog.CreateInstance (__uuidof(Catalog)));
- TESTHR(hr = pTableNew.CreateInstance(__uuidof(Table)));
- TESTHR(hr = pIndexNew.CreateInstance(__uuidof(Index)));
- TESTHR(hr = pIndex.CreateInstance (__uuidof(Index)));
- TESTHR(hr = pColumn.CreateInstance (__uuidof(Column)));
-
- // 连接
- pCatalog->PutActiveConnection(strcnn);
- // 表名
- pTableNew->Name = _bstr_t(strTabName);
- pTableNew->ParentCatalog = pCatalog;
- //
- //m_pTableNew->Columns->Append("ContactId", ::adInteger,0);
- //m_pTableNew->Columns->GetItem("ContactId")->Properties->GetItem("AutoIncrement")->Value = true;
-
- // 加入字段
- CString str;
- str = _T("公司名称");//"公司名称";
- pTableNew->Columns->Append(_variant_t(str), ADOCG::adVarWChar, 50);
- pTableNew->Columns->GetItem(_variant_t(str))->Properties->GetItem("Jet OLEDB:Allow Zero Length")->Value =false; //将必填字段设置为否
- str = _T("产品名称");//"产品名称";
- pTableNew->Columns->Append(_variant_t(str), ADOCG::adVarWChar, 50);
- pTableNew->Columns->GetItem(_variant_t(str))->Properties->GetItem("Jet OLEDB:Allow Zero Length")->Value =false;
- str = _T("产品规格");//"产品名称";
- pTableNew->Columns->Append(_variant_t(str), ADOCG::adVarWChar, 50);
- pTableNew->Columns->GetItem(_variant_t(str))->Properties->GetItem("Jet OLEDB:Allow Zero Length")->Value =false;
- //*
- // 加入主键
- pIndexNew->Name = "pryIndex";//"日期";
- pIndexNew->Columns->Append(_variant_t(str), ADOCG::adVarWChar, 50);
-
- pIndexNew->PutPrimaryKey(-1);
- pIndexNew->PutUnique(-1);
- pTableNew->Indexes->Append(_variant_t ((IDispatch*)pIndexNew));
- //*/
-
- // 加入表中
- pCatalog->Tables->Append(_variant_t ((IDispatch*)pTableNew));
- pCatalog->Tables->Refresh();
-
- }
- catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- AfxMessageBox(e.Description());
- return FALSE;
-
- }
- return TRUE;
- }
- //4.判断一个表在数据库中是否存在
- BOOL CDeRenDlg::IsTalbeExit(CString strTable)
- {
- try
- {
- if(!m_adoConnection.IsOpen()) return FALSE;
-
- _RecordsetPtr pRstSchema = NULL;
- pRstSchema=m_adoConnection.OpenSchema(adSchemaTables);
-
- while(!pRstSchema->adoEOF)
- {
- _bstr_t table_name = pRstSchema->Fields->GetItem("TABLE_NAME")->Value;
- _bstr_t table_type = pRstSchema->Fields->GetItem("TABLE_TYPE")->Value;
- if ( strcmp(((LPCSTR)table_type),"TABLE")==0)
- {
- if(strTable.Compare((LPCSTR)table_name)==0)return true;
- }
-
- pRstSchema->MoveNext();
- }
-
- if(pRstSchema) pRstSchema->Close(); /*&&(pRstSchema->State==ADODB::adStateOpen)*/
- }
- catch(_com_error e)//
- {
- ::MessageBox(NULL,e.Description(),"Error",MB_OK);
- return FALSE;
- }
- return false;
- }