How do you programmatically check for MS Access database table, if not exist then create it?
Answer. When you try to create a table with an already existing table name, you will receive an error message, and no table will be modified or created.
Find blank values. As needed, open the table in Datasheet view that contains the values you want to find. In the table, select the field that contains the blank values that you want to find. On the Home tab, in the Find group, click Find, or press CTRL+F.
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.
You could iterate though the table names to check for a specific table. See the below code to get the table names.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With