VC建立access資料庫

來源:互聯網
上載者:User

ado技術目前已經成為串連資料庫的主流技術,下面我來介紹如何使用ado來動態建立access資料庫。

為了使用ado,必須引入微軟的兩個動態串連庫msadox.dll和msado15.dll:

#pragma warning (disable: 4146)
#import "c:/Program Files/Common Files/system/ado/msadox.dll"
#import "c:/Program Files/Common Files/system/ado/msado15.dll" no_namespace rename("EOF", "EndOfFile")
#pragma warning (default: 4146)

將上述代碼加入到stdafx.h檔案中,由於ado是com組件,因此使用ado之前還要初始化com環境:

CoInitialize(NULL);

下面是一個在access資料庫中建立表的sql語句的例子:

 

HRESULT hr = S_OK;
 CString strcnn(_T("Provider=Microsoft.JET.OLEDB.4.0;Data source = D://test.mdb"));
 try
 {
  ADOX::_CatalogPtr m_pCatalog = NULL;
  hr = m_pCatalog.CreateInstance(__uuidof (ADOX::Catalog));
  if(FAILED(hr))
  {
   _com_issue_error(hr);
  }
        else
        {
   //這裡的路徑可以自己隨意建立 ,我簡單的填寫了一下D://test.mdb13.       
            //m_pCatalog->Create("Provider=Microsoft.JET.OLEDB.4.0;Data source = D://test.mdb");
   m_pCatalog->Create(_bstr_t(strcnn)); //Create MDB
  }
 }
 catch(_com_error &e)
 {
  AfxMessageBox(e.ErrorMessage());
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.