将Excel中的数据导入到SqlServer中,发现decimal(18,2)类型非整数数据比原先小了0.01。
代码如下:
- OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"
- + "Data Source="
- + Server.MapPath("../../uploadfiles/")
- + str + ";Extended Properties=Excel 8.0;");
- OleDbDataAdapter da =
- new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- string rowGuid = Guid.NewGuid().ToString();
- DataRow[] rows =
- dt.Select("[经销商编码(*)] Is Null And [日期(*)] Is Null And [金额(*)] Is Null");
- foreach (DataRow row in rows)
- {
- dt.Rows.Remove(row);
- }
- DataColumn column = new DataColumn("rowGuid");
- column.DefaultValue = rowGuid;
- dt.Columns.Add(column);
- SqlBulkCopy copy =
- new SqlBulkCopy(WebConfigurationManager.AppSettings["ConnectionString"].ToString());
- copy.BatchSize = 1000;
- copy.ColumnMappings.Add(0, "SDDealerCode");
- copy.ColumnMappings.Add(1, "SDDealerName");
- copy.ColumnMappings.Add(2, "PayDate");
- copy.ColumnMappings.Add(3, "Balance");
- copy.ColumnMappings.Add(4, "RowGuid");
- copy.DestinationTableName = "UT_PrePaid_Temp";
- copy.WriteToServer(dt);
调试发现 数据在内存中时正常,进入数据库中后就不对了。
最终有经验的员工给出解决方式:
- DataTable dt = new DataTable();
- dt.Columns.Add("经销商编码(*)");
- dt.Columns.Add("经销商名称");
- dt.Columns.Add("日期(*)");
- dt.Columns.Add("金额(*)", typeof(System.Decimal));
- da.Fill(dt);
强制指定下 金额的类型
问题解决 但是不明白为什么。求解