如何以编程方式检查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. 检查数据库是否存在(MySQL),如果不是在PHP中创建它
- 2. 如何检查MS Access数据库中是否存在指定的列?
- 3. 如何检查在Access数据库中是否存在记录
- 4. 检查访问数据库(.mbd)是否存在,如果不创建它
- 5. PHP检查MS SQL数据库是否存在?
- 6. 在Oracle中创建它之前检查表是否存在
- 7. 检查数据库是否存在
- 8. JavaDB - 检查数据库是否存在
- 9. 检查SQLite数据库是否存在
- 10. MySQL检查数据库是否存在
- 11. 如何检查MS Access数据库在开启前是否有密码保护
- 12. 检查是否在ASP.NET C#MS Access数据库返回的任何行
- 13. C#如何在将大量数据插入MS Access数据库之前检查记录是否存在?
- 14. 检查MS Access 2010是否安装
- 15. 查询MS Access数据库在C#
- 16. 从C#中,如何验证表是否已存在MS Access数据库
- 17. 德尔福:创建没有Ms Access的Access数据库(.mdb)
- 18. 检查CREATE TABLE是否实际创建表或它是否已经存在?
- 19. 导出MS Access数据库后创建更多表单
- 20. 在MS Access中存储数据并在Excel中查询它
- 21. 的MS Access查询创建多个表
- 22. 检查数据库中是否存在数据库
- 23. 检查数据库是否已在Android上创建
- 24. 在创建它之前检查是否存在唯一值
- 25. ms-access:是否可以将访问表单另存为数据库?
- 26. HTML5数据库表 - 检查是否空
- 27. 通过Ajax创建Laravel会话并检查它是否存在
- 28. 使用LINQ检查数据库中是否存在表格
- 29. C#检查数据库中是否存在一个表
- 30. Flask-SQLAlchemy检查数据库中是否存在表格
+1,不仅仅是捕捉错误更优雅。请删除`catch {throw;但是:这是一个NOOP。 – Heinzi 2013-01-24 14:31:30