C# 检查MS Access数据库表(如果不存在)并创建它
如何以编程方式检查MS Access数据库表,如果不存在,则创建它 只需执行以下代码,如果表将存在,它将返回错误,否则它将创建一个新表:C# 检查MS Access数据库表(如果不存在)并创建它,c#,ms-access,C#,Ms Access,如何以编程方式检查MS Access数据库表,如果不存在,则创建它 只需执行以下代码,如果表将存在,它将返回错误,否则它将创建一个新表: try { OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + frmMain.strFilePath + "\\ConfigStructure.mdb"); myConnect
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
}
试试看
{
OleDbConnection myConnection=新的OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+frmMain.strFilePath+”\\ConfigStructure.mdb”);
myConnection.Open();
OleDbCommand myCommand=新的OleDbCommand();
myCommand.Connection=myConnection;
myCommand.CommandText=“创建表()”;
myCommand.ExecuteOnQuery();
myCommand.Connection.Close();
}
捕获(OLEDBEException e)
{
如果(e.ErrorCode==3010 | | e.ErrorCode==3012)
//若出现错误,则表存在,并按要求进行处理
}
如果表已经存在,则返回这些错误代码。对于所有表,您可以迭代表名以检查特定表。请参阅下面的代码以获取表名
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();
}
string connectionstring=“您的连接字符串”;
string[]restrictionValues=新字符串[4]{null,null,null,“TABLE”};
OLEDB连接oleDbCon=新的OLEDB连接(connectionString);
List tableNames=新列表();
尝试
{
oleDbCon.Open();
DataTable schemaInformation=oleDbCon.GetSchema(“表”,限制值);
foreach(schemaInformation.Rows中的数据行)
{
tableNames.Add(row.ItemArray[2].ToString());
}
}
最后
{
oleDbCon.Close();
}
为了完整性起见,我要指出,不久前我发布了4种不同的编码方法。在MSysObjects上运行SQL SELECT的版本可以从外部访问工作,但在某些情况下,您可能会遇到安全错误(因为您不允许访问Jet/ACE系统表)。要检查表是否存在,可以像这样扩展数据库连接:
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;
}
}
然后,您可以在任何派生类(如OleDbConnection、SQLiteConnection或SqlConnection)中调用TableExists
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,比仅仅捕捉错误更优雅。请删除
捕获{throw;}
,不过:这是一个NOOP。