3

我正在SqlClient.SqlBulkCopy尝试将 csv 文件批量复制到数据库中。调用 ..WriteToServer 方法后出现以下错误。

“来自数据源的字符串类型的给定值无法转换为指定目标列的十进制类型。”

这是我的代码,

 dt.Columns.Add("IsDeleted", typeof(byte));
    dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
    foreach (DataRow dr in dt.Rows)
    {
        if (dr["MobileNo2"] == "" && dr["DriverName2"] == "")
        {
            dr["MobileNo2"] = null;
            dr["DriverName2"] = "";
        }
        dr["IsDeleted"] = Convert.ToByte(0);
        dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
    }
    string connectionString = System.Configuration.ConfigurationManager.
                          ConnectionStrings["connectionString"].ConnectionString;
    SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
    sbc.DestinationTableName = "DailySchedule";
    sbc.ColumnMappings.Add("WirelessId", "WirelessId");
    sbc.ColumnMappings.Add("RegNo", "RegNo");
    sbc.ColumnMappings.Add("DriverName1", "DriverName1");
    sbc.ColumnMappings.Add("MobileNo1", "MobileNo1");
    sbc.ColumnMappings.Add("DriverName2", "DriverName2");
    sbc.ColumnMappings.Add("MobileNo2", "MobileNo2");
    sbc.ColumnMappings.Add("IsDeleted", "IsDeleted");
    sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
    sbc.WriteToServer(dt);
    sbc.Close();

在visual studio开发服务器下运行时没有错误,但是在iis下运行时却给了我一个错误.....

这是我的 sql server 表详细信息,

[Id] [int] IDENTITY(1,1) NOT NULL,
[WirelessId] [int] NULL,
[RegNo] [nvarchar](50) NULL,
[DriverName1] [nvarchar](50) NULL,
[MobileNo1] [numeric](18, 0) NULL,
[DriverName2] [nvarchar](50) NULL,
[MobileNo2] [numeric](18, 0) NULL,
[IsDeleted] [tinyint] NULL,
[CreatedDate] [datetime] NULL,
4

2 回答 2

2

你能提供样本数据吗?

听起来像是手机号码之一。Id 从隔离确切的列开始,然后检查源数据。另外,听起来 DataTable 都是字符串值,请尝试键入数据集。

于 2010-05-30T11:48:52.597 回答
2

如果我不得不猜测 - 文化。IIS 使用什么文化,您的桌面应用程序使用什么文化?逗号与句号等。

于 2010-05-30T20:44:37.763 回答