Wilz
Wilz

Reputation: 239

Data table to SQL table losing precision

I have a datatable with values consists of more than 12 decimal places (e.g. 451234.123456789012) that I'm fetching from an Excel sheet. I'm inserting the values from datatable to SQL table using user-defined table.

In my stored procedure I have a statement like,

insert into tblMytable(fld1, fld2..)
Select fld1, fld2,.. from @MyUDT

Issues:

  1. Suppose all the Excel cell type is 'General' I'm getting the full precision in the data table but when I'm inserting this to SQL table I'm losing all except 2 precisions.

  2. Suppose all the Excel cell type is 'Text' I'm getting only 12 digits for the entire value including Non decimal. Decimal part. e.g. For 451234.123456789012, I'm getting 451234.12345 in my C# datatable. And the same I'm getting after inserting to SQL table.

In my UDT the fields are defined as varchar(20), I checked with varchar(45) but no use.

Upvotes: 0

Views: 1150

Answers (1)

Wilz
Wilz

Reputation: 239

I have handled the issue using,

Since I'm getting the full precision while reading the excel type as general to the C# datatable, I created a datatable clone by changing the column type alone as string, so that I'll get the values all string type as well as with full precision, then i'm passing the datatable to SQL 2008 table without losing the precision, now its working fine.

Code:

dt= ds.Tables[0].Clone();
for (ICol = 0; ICol < ds.Tables[0].Columns.Count; ICol++)
    dt.Columns[ICol].DataType = typeof(string);
    foreach (DataRow row in ds.Tables[0].Rows) 
    {
        dt.ImportRow(row); 
}

Upvotes: 2

Related Questions