如何判断ACCESS数据库中表是否存在?
我有动态创建一个数据库coldlead.mdb,然后创建一个表orderlist,第一次运行正常,第二次运行就报错:说表orderlist已存在。我想首先判断一个表是否存在,如果不存在就创建,如果存在就不用创建。
//创建ACCESS表
ADOX.Table table = new ADOX.Table();
table.Name = "orderList";
ADOX.Column column = new ADOX.Column();
column.ParentCatalog = cat;
column.Name = "RecordId";
column.Type = DataTypeEnum.adInteger;
column.DefinedSize = 9;
column.Properties["AutoIncrement"].Value = true;
table.Columns.Append(column, DataTypeEnum.adInteger, 9);//增加一个自动增长的字段
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);//设置主键
table.Columns.Append("Accept Order Date", DataTypeEnum.adDate, 0);//add a column of accept order date
table.Columns.Append("Production Order", DataTypeEnum.adWChar,10);//add a column of Production Order
table.Columns.Append("Product Spec",DataTypeEnum.adWChar,10);//add a column of the product spec
table.Columns.Append("Product Unit",DataTypeEnum.adWChar,10);//add a loumun of the product unit
table.Columns.Append("Product Number",DataTypeEnum.adInteger,3);//add a column of the product number
table.Columns.Append("Delivery Date",DataTypeEnum.adDate,0);//add a column of the delivery date
cat.Tables.Append(table);
MessageBox.Show("数据库表:" + tableName + "已经创建成功!");
cn.Close();
//connect the databank
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source='" + Application.StartupPath + @"\ColdLead.mdb'");
//insert data to the table
string myExecuteQuery = "insert into [orderList]([Accept Order Date],[Production Order],[Product Spec],[Product Unit],[Product Number],[Delivery Date])values(2008-10-10,'wwx','122ls/45r','gr',50,2008-11-20)";
myConnection.Open();
OleDbCommand myCommand=new OleDbCommand (myExecuteQuery,myConnection);
myCommand.ExecuteNonQuery();
OleDbDataAdapter da = new OleDbDataAdapter("select * from orderList", myConnection);
//control data with DataSet
DataSet ds = new DataSet();
da.Fill(ds);
//show the data in the dataGridView
dataGridView1.DataSource = ds.Tables[0].DefaultView;
//close the databank connect
myConnection.Close();