如何以編程方式檢查MS Access數據庫表是否存在,然後創建它?檢查MS Access數據庫表是否存在創建它
8
A
回答
7
簡單的執行下面的代碼,如果表將存在,它將返回其他方法錯誤,將創建一個新的:如果一個表已經存在
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
}
這些錯誤代碼返回 - check here所有。
12
您可以遍歷表名來檢查特定的表。請參閱下面的代碼來獲取表名。
string connectionstring = "Your connection string";
string[] restrictionValues = new string[4]{null,null,null,"TABLE"};
OleDbConnection oleDbCon = new OleDbConnection(connectionString);
List<string> tableNames = new List<string>();
try
{
oleDbCon.Open();
DataTable schemaInformation = oleDbCon.GetSchema("Tables", restrictionValues);
foreach (DataRow row in schemaInformation.Rows)
{
tableNames.Add(row.ItemArray[2].ToString());
}
}
finally
{
oleDbCon.Close();
}
1
爲了完整起見,我會指出,前陣子我貼4種不同的編碼了TableExists() function within Access的方式。在MSysObjects上運行SQL SELECT的版本可以從外部訪問,儘管在某些情況下,您可能會遇到安全錯誤(因爲您不允許訪問Jet/ACE系統表)。
8
要檢查表存在,你可以這樣延伸的DbConnection:
public static class DbConnectionExtensions
{
public static bool TableExists(this DbConnection conn, string table)
{
conn.open();
var exists = conn.GetSchema("Tables", new string[4] { null, null, table, "TABLE" }).Rows.Count > 0;
conn.close();
return exists;
}
}
然後就可以調用TableExists在這樣的OleDbConnection,SQLiteConnection或SqlConnection的任何派生類。
1
一個簡單的方法做,這是
public bool CheckTableExistance(string TableName)
{
// Variable to return that defines if the table exists or not.
bool TableExists = false;
// Try the database logic
try
{
// Make the Database Connection
ConnectAt();
// Get the datatable information
DataTable dt = _cnn.GetSchema("Tables");
// Loop throw the rows in the datatable
foreach (DataRow row in dt.Rows)
{
// If we have a table name match, make our return true
// and break the looop
if (row.ItemArray[2].ToString() == TableName)
{
TableExists = true;
break;
}
}
//close database connections!
Disconnect();
return TableExists;
}
catch (Exception e)
{
// Handle your ERRORS!
return false;
}
}
相關問題
- 1. 檢查Access數據庫中的表是否存在
- 2. 檢查數據庫是否存在(MySQL),如果不是在PHP中創建它
- 3. 如何檢查MS Access數據庫中是否存在指定的列?
- 4. 如何使用vb.net檢查ms access數據庫中是否存在記錄?
- 5. 如何通過Delphi檢查MS Access(數據庫)中是否存在記錄?
- 6. 檢查MySQL表是否存在以及是否創建它們
- 7. Yii在創建之前檢查數據庫列是否存在
- 8. 如何檢查在Access數據庫中是否存在記錄
- 9. [VB.NET] [ACCESS]如何檢查數據庫是否存在?
- 10. 檢查訪問數據庫(.mbd)是否存在,如果不創建它
- 11. PHP檢查MS SQL數據庫是否存在?
- 12. 檢查表是否新創建或不在數據庫中
- 13. 在Oracle中創建它之前檢查表是否存在
- 14. 創建鏈接表在MS-Access數據庫的Web服務器
- 15. MySQL檢查數據庫是否存在
- 16. JavaDB - 檢查數據庫是否存在
- 17. 檢查SQLite數據庫是否存在
- 18. 檢查數據庫是否存在
- 19. 檢查一個表是否存在,如果它不存在,創建它... iOS/SQLite
- 20. 如何檢查MS Access數據庫在開啓前是否有密碼保護
- 21. 檢查是否在ASP.NET C#MS Access數據庫返回的任何行
- 22. C#如何在將大量數據插入MS Access數據庫之前檢查記錄是否存在?
- 23. 檢查MS Access是否安裝
- 24. 檢查MS Access 2010是否安裝
- 25. MS ACCESS通過數據庫屬性創建新的數據庫?
- 26. 是否有Hibernate API來檢查數據庫是否被創建?
- 27. 如何檢查數據庫表是否存在或不存在?
- 28. 在.NET中創建MS Access數據庫而不需要安裝MS Access?
- 29. 查詢MS Access數據庫在C#
- 30. MS Access數據庫
+1,不僅僅是捕捉錯誤更優雅。請刪除`catch {throw;但是:這是一個NOOP。 – Heinzi 2013-01-24 14:31:30