Foxpro:通过vfpoledb检查表是否存在

发布于 2024-09-13 02:34:24 字数 232 浏览 7 评论 0 原文

我通过 System.Data.OleDb (vfpoledb.dll) 访问 .dbf 文件中的数据。如何通过SQL命令判断表是否存在? SQL Server 上类似于以下内容:

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END

I access data in .dbf files via System.Data.OleDb (vfpoledb.dll). How can I find out whether table exists via SQL command? Something similar to the following on SQL server:

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金橙橙 2024-09-20 02:34:24

如果您有 dbc 文件,您可以查询它以查看该表是否存在。

string dbc = "northwind.dbc";

using (OleDbConnection conn = new OleDbConnection(connectionString)) {
    DataTable dt = new DataTable();
    string sql = string.Format(@"SELECT * FROM {0} WHERE ALLTRIM(ObjectType) = 'Table' AND UPPER(ALLTRIM(ObjectName)) = '{1}'", dbc, tableName.ToUpper());
    OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
    da.Fill(dt);
    bool tableExists = dt != null && dt.Rows.Count == 1;
}

但实际上您不需要 sql 命令或 dbc 文件来获取该信息。您可以使用 GetSchema 方法直接从 OleDbConnection 获取它。

using (OleDbConnection conn = new OleDbConnection(connectionString)) {
    conn.Open();
    DataTable tables = conn.GetSchema("Tables");
    conn.Close();

    var tableExists = (from row in tables.AsEnumerable()
                        where row.Field<string>("Table_Name").Equals(tableName, StringComparison.CurrentCultureIgnoreCase)
                        select row.Field<string>("Table_Name")).FirstOrDefault() != null;
}

If you have a dbc file you can query it to see if the table exists.

string dbc = "northwind.dbc";

using (OleDbConnection conn = new OleDbConnection(connectionString)) {
    DataTable dt = new DataTable();
    string sql = string.Format(@"SELECT * FROM {0} WHERE ALLTRIM(ObjectType) = 'Table' AND UPPER(ALLTRIM(ObjectName)) = '{1}'", dbc, tableName.ToUpper());
    OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
    da.Fill(dt);
    bool tableExists = dt != null && dt.Rows.Count == 1;
}

But really you don't need a sql command or a dbc file to get that information. You can get it straight from the OleDbConnection using the GetSchema method.

using (OleDbConnection conn = new OleDbConnection(connectionString)) {
    conn.Open();
    DataTable tables = conn.GetSchema("Tables");
    conn.Close();

    var tableExists = (from row in tables.AsEnumerable()
                        where row.Field<string>("Table_Name").Equals(tableName, StringComparison.CurrentCultureIgnoreCase)
                        select row.Field<string>("Table_Name")).FirstOrDefault() != null;
}
白首有我共你 2024-09-20 02:34:24

此外,如果您连接到的 DBF 表是“免费”表,并且实际上不是连接的“数据库”(.dbc) 的一部分,那么您只需检查文件是否存在...例如在 C# 中通过

if( File.Exists( PathToTheDatabaseDirectory + TableYouExpect + ".DBF" ))
   file is there
else
   file is missing

Additionally, if you are connecting to DBF tables that are "FREE" tables and NOT actually part of a connected "database" (.dbc), then you can just check for the file's existence or not... Such as in C# via

if( File.Exists( PathToTheDatabaseDirectory + TableYouExpect + ".DBF" ))
   file is there
else
   file is missing
沧笙踏歌 2024-09-20 02:34:24

我不知道如何仅使用 SQL 来执行此操作,但也许您可以使用 File.Exists Method 或者您可以编写一些代码来使用 OleDb 类检查 dbf 是否存在:

private bool DbfExists(string dbfName, string connectionString)
{
    bool dbfExists = true;

    using(OleDbConnection conn = new OleDbConnection(connectionString))
    {
        string sql = string.Format("SELECT * FROM {0}", dbfName);

        using(OleDbCommand command = new OleDbCommand(sql, conn))
        {
            OleDbDataReader reader = null;

            try
            {
                conn.Open();
                reader = command.ExecuteReader();
            }
            catch(Exception ex)
            {
                dbfExists = false;
            }
            finally
            {
                conn.Close();
                reader = null;
            }
        }
    }

    return dbfExists;
}

我还没有尝试编译此代码,因此它可能需要稍微调整一下。

I don't know how to do it only using SQL but maybe you could check for the existence of the file on disk using the File.Exists Method or you could write some code to check for the existence of the dbf using the OleDb classes:

private bool DbfExists(string dbfName, string connectionString)
{
    bool dbfExists = true;

    using(OleDbConnection conn = new OleDbConnection(connectionString))
    {
        string sql = string.Format("SELECT * FROM {0}", dbfName);

        using(OleDbCommand command = new OleDbCommand(sql, conn))
        {
            OleDbDataReader reader = null;

            try
            {
                conn.Open();
                reader = command.ExecuteReader();
            }
            catch(Exception ex)
            {
                dbfExists = false;
            }
            finally
            {
                conn.Close();
                reader = null;
            }
        }
    }

    return dbfExists;
}

I have not tried compiling this code so it may need to be tweaked a bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文