Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I load floating point data from Excel in my C# software using SQL the data ends up being truncated.

For example, instead of 0,054034 I end up with 0,054.

What I have tried:

The relevant part of my code is:

DataTable part8 = new DataTable();
part8 = loadData(path, "SELECT * FROM [Gaszähler$X13:Y]");
Console.WriteLine(part8.Rows[10][1]);

private DataTable loadData(String path, String command)
{
    String filepath = path;
    String db_command = command;

    OleDbDataAdapter adapter = fetch(filepath, db_command);
    DataSet set = new DataSet();
    DataTable returntable = new DataTable();
    adapter.Fill(set, "table1");
    returntable = set.Tables["table1"];

    return returntable;
}

public OleDbDataAdapter fetch(string filepath, string com)
{
    OleDbConnection conn = new OleDbConnection(); // Die Verbindung
    string ConStr = // Der Connectionstring
         @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + filepath
        + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";

    conn.ConnectionString = ConStr; //Der Connectionstring wird gesetzt

    OleDbCommand command = new OleDbCommand // Das OleDb Kommando
        (
            com, conn
        );
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(command); // Ein Adapter

    return myAdapter;
}
Posted
Updated 26-Apr-18 3:34am

1 solution

The values are not truncated but printed with a default precision when using WriteLine(double) which calls Double.ToString Method (System)[^].

To specify the precision use one of the Double.ToString() overloads that accept a format string like Double.ToString Method (String) (System)[^].
 
Share this answer
 
Comments
Member 13799709 26-Apr-18 10:29am    
The value is actually truncated, in another function I am passing it to another excel table where it is short of a few digits
Jochen Arndt 26-Apr-18 10:37am    
It is not truncated as long as it is read and stored as double. But it will be truncated when converted to a string like with printing or passing as string to an export interface.

If you for example use a database interface for export (like OLEDB), you have to use parametrised commands to ensure that it is passed as double and pass it as such and not as string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900