检查MS Access数据库表(如果不存在)创建它

时间:2011-01-25 12:34:56

标签: c# ms-access

如何以编程方式检查MS Access数据库表,如果不存在则创建它?

5 个答案:

答案 0 :(得分:13)

您可以遍历表名以检查特定表。请参阅以下代码以获取表名。

        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 :(得分:9)

要检查表是否存在,您可以像这样扩展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;
    }
}

然后,您可以在OleDbConnection,SQLiteConnection或SqlConnection等任何派生类中调用TableExists。

答案 2 :(得分:8)

如果表存在,只需执行以下代码,否则将返回错误,它将创建一个新的:

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表示所有错误代码。

答案 3 :(得分:2)

一种简单的方法是

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;
        }
    }

答案 4 :(得分:1)

为了完整起见,我会指出前一段时间我发布了4种编码TableExists() function within Access的方法。在MSysObjects上运行SQL SELECT的版本可以从Access外部工作,但在某些情况下,您可能会收到安全性错误(因为您不允许访问Jet / ACE系统表)。