0

I am trying to create a dbf table from a query to the database, made from a program written in C #.

I have followed the following thread: Create .DBF file from SQL table records

For this the project has included another one of these, called DbfLibrary. In an example reduced to my problem, I do the following:

    static void Main(string[] args)
    {
        var fhaltaColumn = DbfFieldDescriptors.GetDateTimeField("fhalta");

        var columns = new List<DbfFieldDescriptor>() {fhaltaColumn};

        Func<MyClass, object> mapfhalta = myClass => myClass.fhalta;

        var mapping = new List<Func<MyClass, object>>() { mapfhalta };

        List<MyClass> values = new List<MyClass>();

        values.Add(new MyClass()
        {
            fhalta = DateTime.Now
         });

        string fileName = "C:\\temp\\Archivo.dbf";
        DbfFileFormat.Write(@fileName, values, mapping, columns, Encoding.Default);
    }
    public class MyClass
    {
        public DateTime fhalta { get; set; }
    } 

The result is a dbf file, with a field named fhalta, but the value is "/ /:: AM" and not DateTime.Now as I need.

How do I pass a datetime value correctly? What am I doing wrong?

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • Specify the type in the parameter : new OleDbParameter( "parmDetails", OleDbType.Date) – jdweng Dec 03 '19 at 18:51
  • I do not understand. Why an OleDbParameter? – Santiago Espósito Dec 04 '19 at 15:08
  • The link you posted has examples of oldbeparameters. You did not post the code with your query that should contain the parameters. – jdweng Dec 04 '19 at 15:37
  • Actually, the method I am using and with which I cannot pass the DateTime type data correctly is "The Second Write Method". – Santiago Espósito Dec 04 '19 at 17:29
  • You need to write to database using parameters. The driver (similar to excel general formatting) is guessing at the type when you do not use a parameter. So the drive may thing you are writing a number database instead of a date and changing the value. – jdweng Dec 04 '19 at 17:49

0 Answers0