C# OleDb判断ACCESS中是否存在表
添加引用
代码加入引用
代码如下
/// <summary> /// 公有数据库连接对象 /// </summary> public OleDbConnection UseConn { get { return useConn; } set { useConn = value; } } /// <summary> /// 指定路径初始化数据库连接对象 /// </summary> /// <param name="sqlPath">数据库路径</param> /// <param name="sqlName">数据库名称</param> public AccessSQL(string sqlPath, string sqlName) { useConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sqlPath + sqlName + ".mdb"); OpenSQL(); } /// <summary> /// 打开数据库连接 /// </summary> /// <returns>正常打开返回true,异常返回false</returns> public bool OpenSQL() { try { if (useConn.State == System.Data.ConnectionState.Closed) { useConn.Open(); } return true; } catch (Exception ex) { MessageBox.Show("连接数据库时发生异常:\r\n\r\n" + ex.Message); return false; } } /// <summary> /// 判断某个表是否存在 /// </summary> /// <param name="tableName">表名</param> /// <returns>存在返回true,不存在返回false</returns> public bool IsExitsTables(string tableName) { DataTable schemaTable = UseConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); if (schemaTable == null) { return false; } else { for (int r = 0; r < schemaTable.Rows.Count; r++) { string colName = schemaTable.Rows[r]["TABLE_NAME"].ToString(); if (colName == tableName) { return true; } } return false; } }