Link to home
Create AccountLog in
Avatar of ekberglinda
ekberglindaFlag for Sweden

asked on

How do I fill a dBase IV numeric field with a number without getting decimal points in the file?

I try to create a dbf file (dBase 4) from C# using an OleDb connection as shown in code snippet.
When I run it the file is created but the number field is written with 5 decimal points.
How do I get rid of the decimal points in the file?
I've tried replacing the number with a variable of different types (int, decimal, string with no decimal formatting etc.) but nothing works.
public string CreateDbfFile()
{
    OleDbConnection oleDbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\TEMP\\DBF;Extended Properties=dBASE IV;");
 
    try
    {
        oleDbConn.Open();
 
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "CREATE table Test2 (Name CHAR(50), myNum NUMBER);";
        cmd.Connection = oleDbConn;
        cmd.ExecuteNonQuery();
 
        cmd = new OleDbCommand();
        cmd.Connection = oleDbConn;
        cmd.CommandText = "INSERT INTO Test2 VALUES('John Smith', 1234567890);";
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
    finally
    {
        oleDbConn.Close();
    }
 
    return "Wohoo";
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ekberglinda
ekberglinda
Flag of Sweden image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer