Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果存在,如何在Access中删除表_C#_Ms Access - Fatal编程技术网

C# 如果存在,如何在Access中删除表

C# 如果存在,如何在Access中删除表,c#,ms-access,C#,Ms Access,我使用C#和access db。 mysql中有一个用于删除表的语句,如下所示: drop table if exists t_table 那么你知道Access的类似声明吗 我不知道Access中这样的SQL语句。 但是,您可以执行以下操作之一: 尝试删除表,但不检查是否存在,捕获异常(若未找到表,则应具有特定代码)并忽略它 如果表存在,请尝试签入Access隐藏表MSysObjects(使用ADO,默认情况下它没有权限) 使用下面的代码(糟糕的是:删除表不会返回受影响的记录): 作为

我使用C#和access db。
mysql中有一个用于删除表的语句,如下所示:

drop table if exists t_table
那么你知道Access的类似声明吗


我不知道Access中这样的SQL语句。
但是,您可以执行以下操作之一:

  • 尝试删除表,但不检查是否存在,捕获异常(若未找到表,则应具有特定代码)并忽略它

  • 如果表存在,请尝试签入Access隐藏表MSysObjects(使用ADO,默认情况下它没有权限)

  • 使用下面的代码(糟糕的是:删除表不会返回受影响的记录):

作为答案发布的SQL应该满足您的需要
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;data source=c:\myDatabase.mdb"))
{
    conn.Open();

    string tableToDelete = "myTable";   //table name
    bool tableExists = false;

    DataTable dt = conn.GetSchema("tables");

    foreach (DataRow row in dt.Rows)
    {
        if (row["TABLE_NAME"].ToString() == tableToDelete)
        {
            tableExists = true;
            break;
        }
    }

    if (tableExists)
    {
        using (OleDbCommand cmd = new OleDbCommand(string.Format("DROP TABLE {0}", tableToDelete), conn))
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Table deleted");
        }
    }
    else
        MessageBox.Show(string.Format("Table {0} not exists", tableToDelete));
}