I'm using the below code to connect to an Access Database using OleDb
connection in C# .Net
How can I know if the table that I have hard-coded into the program actually exists in the file, so that I can show the user the appropriate message?
try
{
var dbSource = "Data Source = " + source;
const string dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;";
using (var con = new OleDbConnection())
{
con.ConnectionString = dbProvider + dbSource;
con.Open();
using (var cmd = new OleDbCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM [Concrete Design Table 1]";
// How do I know the table name is valid? It results in errors when it is not?
// How to prevent it?
using (var dataReader = cmd.ExecuteReader())
{
while (dataReader != null && dataReader.Read())
{
// read the table here
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
var schema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
, if I am understanding what you want correctly. – Natal