value getting changed during SqlBulkCopy

asp.net c# csv sqlbulkcopy sql-server

Question

Using SqlBulkCopy, I'm attempting to upload some data from a csv file to a SQL server. The numbers in this case change automatically, for example, 0.9824 changes to 0.982400000095367.

This is the code I'm using.

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strConnectionString, SqlBulkCopyOptions.FireTriggers))
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("dt", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("wreb", typeof(string)));
            dt.Columns.Add(new DataColumn("zid", typeof(int)));
            dt.Columns.Add(new DataColumn("zone_name", typeof(string)));
            dt.Columns.Add(new DataColumn("region", typeof(string)));
            dt.Columns.Add(new DataColumn("iloss", typeof(float)));
            dt.Columns.Add(new DataColumn("dloss", typeof(float)));

            for (int i = 0; i < DataDS.Tables[0].Rows.Count; i++)
            {
                DataRow myDr = dt.NewRow();
                myDr["dt"] = DateTime.ParseExact(DataDS.Tables[0].Rows[i]["dt"].ToString(), "d/M/yyyy", null);
                myDr["wreb"] = DataDS.Tables[0].Rows[i]["wreb"].ToString();
                myDr["zid"] = DataDS.Tables[0].Rows[i]["zid"].ToString();
                myDr["zone_name"] = DataDS.Tables[0].Rows[i]["zone_name"].ToString();
                myDr["region"] = DataDS.Tables[0].Rows[i]["region"].ToString();
                myDr["iloss"] = DataDS.Tables[0].Rows[i]["iloss"].ToString();
                myDr["dloss"] = DataDS.Tables[0].Rows[i]["dloss"].ToString();
                dt.Rows.Add(myDr);
                myDr = null;
            }
            bulkCopy.BulkCopyTimeout = 0;
            bulkCopy.ColumnMappings.Add("dt", "Zdate");
            bulkCopy.ColumnMappings.Add("wreb", "Entity");
            bulkCopy.ColumnMappings.Add("zid", "ZoneID");
            bulkCopy.ColumnMappings.Add("zone_name", "Zone_Name");
            bulkCopy.ColumnMappings.Add("region", "region");
            bulkCopy.ColumnMappings.Add("iloss", "iloss");
            bulkCopy.ColumnMappings.Add("dloss", "dloss");
            bulkCopy.DestinationTableName = "tbl_zloss";
            bulkCopy.WriteToServer(dt);
        }
1
1
4/18/2014 2:02:53 PM

Accepted Answer

It seems like you are using the erroneous datatype float.
To get precise values, use decimal. Depending on your data, precision and scale

4
4/18/2014 2:06:19 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow