开发者

Reading DBF with VFPOLEDB driver

开发者 https://www.devze.com 2022-12-20 04:29 出处:网络
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:

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 (开发者_C百科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;
    }
}


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

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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消