在尝试使用以下代码在现有CDX
文件上创建DBF
文件时,我收到一个例外情况“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文件以避免异常“当前工作区中没有表格打开”,同时使用Microsoft OLE DB Provider
Visual FoxPro
答案 0 :(得分:2)
您正在隐藏最重要的部分,即CreateCDXQueries的代码。在尝试创建索引之前的某个地方,您应该将当前工作区中的表打开为独占。此外,从连接字符串中删除该扩展属性部分。 这是一个非常基本的例子。假设您已经使用此命令创建了一个表(或在同一个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();
}
没有尝试...抓住代码,因为它是一个快速的样本。
答案 1 :(得分:1)
这个与您的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)
{
// ...
}
}
}