当前工作区中没有打开 table

No table is open in the current work area

在尝试使用以下代码在现有 DBF 文件上创建 CDX 文件时,我收到异常提示“No table is open in the current work area".

语言 - C#

异常详细信息 -

Message - No table is open in the current work area.
Source - Microsoft OLE DB Provider for Visual FoxPro

下面是代码片段:

     public void CreateIndex()
    {
        var cdxExpressions = new List<CDXExpression> {
            new CDXExpression { expression = "ENTRY_DATE", name = "cdx_p1"},
            new CDXExpression { expression = "ENTRY_CODE", name = "cdx_p2"},
            new CDXExpression { expression = "Month", name = "cdx_p3"},
            new CDXExpression { expression = "Year", name = "cdx_p4"},
            new CDXExpression { expression = "Company", name = "cdx_p5"}
        };

        string dbfFile = ConfigurationManager.AppSettings["DBFFileLocation"];
        string connectionString = @"Provider=VFPOLEDB.1;Data Source=" + dbfFile + ";Extended Properties=dBASE IV;";
        OleDbConnection connection = new OleDbConnection(connectionString);

        var queries = DBFQuery.CreateCDXQueries(cdxExpressions, dbfFile);

        connection.Open();
        try
        {
            foreach (var qry in queries)
            {
                OleDbParameter script = new OleDbParameter("script", qry);
                OleDbCommand dbfCommand = connection.CreateCommand();
                dbfCommand.CommandType = CommandType.StoredProcedure;
                dbfCommand.CommandText = "ExecScript";
                //dbfCommand.Connection.Open();
                dbfCommand.Parameters.Add(script);
                dbfCommand.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            // I should Open the DBF/Table in the current work area to avoid the exception
        }
        connection.Close();
    }

如何在 C# 中打开 DBF 文件以避免出现异常 "No table is open in the current work area",也将 Microsoft OLE DB Provider 用于 Visual FoxPro

您隐藏了最重要的部分,CreateCDXQueries 的代码。在尝试创建索引之前,您应该在当前工作区中以独占方式打开 table。此外,从连接字符串中删除该扩展属性部分。 这是一个非常基本的例子。假设您已经使用此命令创建了一个 table(或在同一个 Execscript 调用中创建):

Create Table ('d:\temp\IndexTest') free (id int, firstName c(10), lastName c(10))

这是创建索引标签的 C# 代码:

static void Main()
{
    string myCode =
    @"use indexTest exclusive
index on firstName tag firstName
index on lastName tag LastName
index on id tag id
use
";

    string strCon = @"Provider=VFPOLEDB;Data Source=d:\temp";
    OleDbConnection con = new OleDbConnection(strCon);
    OleDbCommand cmd = con.CreateCommand();

    con.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "ExecScript";
    OleDbParameter parm = cmd.CreateParameter();
    parm.OleDbType = OleDbType.Char;
    cmd.Parameters.Add(parm);

    parm.Value = myCode;

    cmd.ExecuteNonQuery();
    con.Close();
}

代码中没有 try...catch,因为它是一个快速示例。

这个是你的 CDXExpression 列表:

public void CreateIndex()
{
    var cdxExpressions = new List<CDXExpression> {
            new CDXExpression { expression = "ENTRY_DATE", name = "cdx_p1"},
            new CDXExpression { expression = "ENTRY_CODE", name = "cdx_p2"},
            new CDXExpression { expression = "Month", name = "cdx_p3"},
            new CDXExpression { expression = "Year", name = "cdx_p4"},
            new CDXExpression { expression = "Company", name = "cdx_p5"}
        };

    var myCode = "use sample exclusive\n" +
          string.Join("\n", cdxExpressions.Select(e => $"index on {e.expression} tag {e.name}")) +
          "\nuse";

    string dbfFile = ConfigurationManager.AppSettings["DBFFileLocation"];
    string connectionString = @"Provider=VFPOLEDB;Data Source=" + dbfFile;

    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand cmd = connection.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ExecScript";
        OleDbParameter parm = cmd.CreateParameter();
        parm.OleDbType = OleDbType.Char;
        cmd.Parameters.Add(parm);
        parm.Value = myCode;
        try
        {
            connection.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            // ...
        }
    }
}