值在SqlBulkCopy期间发生了变化

时间:2014-04-18 14:02:53

标签: c# asp.net sql-server csv sqlbulkcopy

我正在尝试使用SqlBulkCopy将一些数据从csv文件上传到sql server 这里的问题是值会自动更改 例如0.9824变为0.982400000095367

以下是我正在使用的代码

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 个答案:

答案 0 :(得分:4)

看起来你正在使用float,这是一种不精确的数据类型 使用decimal获取准确值。精度和规模取决于您的数据