Reading DBF with VFPOLEDB driver

只谈情不闲聊 提交于 2019-12-01 16:41:37

问题


I am using VFPOLEDB driver to read DBF files and I keep getting this error and I am not sure why and how to fix the problem:

The provider could not determine the Decimal value. For example, the row was just created, the default for the Decimal column was not available, and the consumer had not yet set a new Decimal value.

Here is the code. I call this routine to return a DataSet of the DBF file and display the data in a DataGridView.

public DataSet GetDBFData(FileInfo fi, string tbl)
{
    using (OleDbConnection conn = new OleDbConnection(
    @"Provider=VFPOLEDB.1;Data Source=" + fi.DirectoryName + ";"))
    {
        conn.Open();
        string command = "SELECT * FROM " + tbl;
        OleDbDataAdapter da = new OleDbDataAdapter(command, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
}

回答1:


I found the solution here: Error reading certain numeric values with VFPOLEDB driver

SELECT CAST(FieldName As NUMERIC(11, 3)) From TableName



回答2:


I finally solved the problem by getting the table schema and then casting all of non-character fields to varchar in the select statement. Good enough for previewing the contents of the table.




回答3:


It is a known issue. Especially, if You need to select all columns, it is much more comfortable:

Select * from some_table

One working solution is to use another provider, for example Microsoft.Jet.OLEDB.4.0. Example connection string can be found here: http://docs.30c.org/conn/dbf-foxpro.html




回答4:


If you add a row from your gridview, it doesn't necessarily use a default value, but rather NULLs, so you may need to pre-set your defaults, or set the schema to NOT Allow Nulls.

You could automate through the columns after the query is done and force defaults based on the columns data types, such as

foreach (DataColumn oDC in YourDataSet.Tables[0].Columns)
{
   if (oDC.DataType.ToString().Contains("String"))
    oDC.DefaultValue = "";
   else if (oDC.DataType.ToString().Contains("Int32"))
    oDC.DefaultValue = 0;
   else if (oDC.DataType.ToString().Contains("DateTime"))
    oDC.DefaultValue = DateTime.MinValue;
}

these are just 3 default types, but there could be others like boolean, decimal, float, whatever, just add into the if/else and put whatever "default" values. It MAY help where otherwise "NULL" values are getting injected in when adding new rows.



来源:https://stackoverflow.com/questions/2277970/reading-dbf-with-vfpoledb-driver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!