簡體   English   中英

檢查OleDb數據庫是否存在

[英]Check if OleDb database exists

我試圖環顧四周,但找不到任何人(甚至在SO中)誰解釋了如何使用OleDb 12.0檢查Access數據庫是否存在。 我想出了如何創建數據庫:

Catalog cat = new CatalogClass();
string cntPath = System.IO.Directory.GetCurrentDirectory();
string createStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + cntPath + "\\" + TB_LoginUsername.Text.ToLower() + "_LOG.accdb;";
cat.Create(createStr);
Table tbl = new Table();
tbl.Name = TB_LoginUsername.Text + "_SESSIONS";
tbl.Columns.Append("ID", DataTypeEnum.adGUID);
tbl.Columns.Append("Cycling", DataTypeEnum.adVarWChar, 25);
tbl.Columns.Append("Running", DataTypeEnum.adVarWChar, 25);
tbl.Columns.Append("Swimming", DataTypeEnum.adVarWChar, 25);
cat.Tables.Append(tbl);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(tbl);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.Tables);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);

但是我不知道如何檢查一個人是否存在-有人可以指導我該怎么做嗎? 我懷疑這與File.Exists()但我不知道如何使用它。

謝謝!

這是檢查表是否存在的工作代碼:

try
{
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + frmMain.strFilePath + "\\ConfigStructure.mdb");
        myConnection.Open();
        OleDbCommand myCommand = new OleDbCommand();
        myCommand.Connection = myConnection;
        myCommand.CommandText = "CREATE TABLE <yourtable name>(<columns>)";
        myCommand.ExecuteNonQuery();
        myCommand.Connection.Close();
}
catch(OleDbException e)
{  
    if(e.ErrorCode == 3010 || e.ErrorCode == 3012)
    // if error then table exist do processing as required
}

更新

如果要檢查數據庫是否存在:可以使用DTO,獲取數據庫名稱列表,檢查所需名稱,然后做出適當反應,或者可以打開ADO連接。 如果打開連接時出現錯誤,提示“未找到未指定數據源的默認驅動程序”(來自ODBC)或“ [LNA] [普及] [ODBC引擎接口] [數據記錄管理器]找不到指定的命名數據庫( Btrieve Error 2301)”(來自OLEDB),則數據庫不存在。

您可以使用DTO創建它(如果不存在)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM