如何使用C#中的OLEDB驱动程序使用.NDX索引文件查询Foxpro .DBF文件

时间:2015-11-16 23:17:19

标签: c# oledb visual-foxpro foxpro oledbconnection

我有一个Foxpro .DBF文件。我正在使用OLEDB驱动程序来读取.DBF文件。我可以查询DBF并利用它的.CDX索引文件(因为它会自动打开)。我的问题是我想用.NDX索引文件(在打开.DBF时不会自动打开)查询它。如何使用OLEDB驱动程序在C#中打开.NDX文件导致DBF在没有索引的情况下搜索记录真的很大?谢谢大家!这是我用来阅读DBF的代码。

OleDbConnection oleDbConnection = null;
        try
        {
            DataTable resultTable = new DataTable();
            using (oleDbConnection = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=P:\\Test\\DSPC-1.DBF;Exclusive=No"))
            {
                oleDbConnection.Open();
                if (oleDbConnection.State == ConnectionState.Open)
                {
                    OleDbDataAdapter dataApdapter = new OleDbDataAdapter();
                    OleDbCommand command = oleDbConnection.CreateCommand();

                    string selectCmd = @"select * from P:\Test\DSPC-1  where dp_file = '860003'";
                    command.CommandType = CommandType.Text;
                    command.CommandText = selectCmd;

                    dataApdapter.SelectCommand = command;
                    dataApdapter.Fill(resultTable);
                    foreach(DataRow row in resultTable.Rows)
                    {
                        //Write the data of each record 
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            try
            {
                oleDbConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to close Oledb connection: " + e.Message);
            }
        }

2 个答案:

答案 0 :(得分:2)

您的连接字符串应仅引用.dbf文件所在的PATH。

然后,您的查询只是表名。

new OleDbConnection("Provider=VFPOLEDB.1;Data Source=P:\\Test\\;Exclusive=No"))

selectCmd = @"select * from DSPC-1  where dp_file = '860003'";

至于使用.NDX,如何/在哪里创建...这是一个旧的dBASE文件,您正在使用Visual Foxpro驱动程序?

如果它是如上所述的单独的,您可能需要通过ExecScript()来首先使用索引显式打开文件,然后运行您的查询。这只是一个带有您固定值的示例。你可能不得不PARAMETERIZE它,否则你会接受sql-injection。

cmd.CommandText = string.Format(
@"EXECSCRIPT('
USE DSPC-1 INDEX YourDSPC-1.NDX
SELECT * from DSPC-1 where dp_file = '860003'" );

另外,您的表名可能会出现连字符问题,您可能需要将其包装在[方括号]中,但如果是问题则不能正确。

答案 1 :(得分:2)

默认情况下不会打开ndx文件,这些文件已成为过去,为什么不简单地将索引添加到CDX中。如果它不是一个选项,那么DRapp的ExecScript建议就是你能做的。他很亲密。这是你如何做到的:

string myCommand = @"Use ('P:\Test\DSPC-1') alias myData
Set Index To ('P:\Test\DSPC-1_Custom.NDX')
select * from myData ;
  where dp_file = '860003' ;
  into cursor crsResult ;
  nofilter
SetResultset('crsResult')";

DataTable resultTable = new DataTable();
using (oleDbConnection = new OleDbConnection(@"Provider=VFPOLEDB;Data Source=P:\Test"))
{
  oleDbConnection.Open();
  OleDbCommand command = new OleDbCommand("ExecScript", oleDbConnection);
  command.CommandType = CommandType.StoredProcedure;
  command.Parameters.AddWithValue("code", myCommand);

  resultTable.Load(cmd.ExecuteReader());
  oleDbConnection.Close();
}