Check for MS Access database table if not exist create it

后端 未结 5 1745
失恋的感觉
失恋的感觉 2020-12-20 14:55

How do you programmatically check for MS Access database table, if not exist then create it?

5条回答
  •  情书的邮戳
    2020-12-20 15:21

    To check if a table exists you can extend DbConnection like this:

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

    Then you can call TableExists in any derived class like OleDbConnection, SQLiteConnection or SqlConnection.

提交回复
热议问题