c# - 为什么 sqlbulkcopy 在 iis 下运行的 asp.net 网站中有这种奇怪的行为?

标签 c# asp.net iis sqlbulkcopy

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

"The given value of type String from the data source cannot be converted to type decimal of the specified target column."

这是我的代码,

 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 developement server下运行没有报错,在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,

最佳答案

你能提供样本数据吗?

听起来像是其中一个手机号码。我从隔离确切的列开始,然后检查源数据。另外,DataTable 听起来好像全是字符串值,请尝试输入数据集。

关于c# - 为什么 sqlbulkcopy 在 iis 下运行的 asp.net 网站中有这种奇怪的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2938108/

相关文章:

c# - 什么是 Visual Studio SDK 中 VirtualSnapshotSpan 中的虚拟空间

c# - 如何合并 ThreadLocal<T> 中的所有值?

C# 单元测试 - Thread.Sleep(x) - 如何模拟系统时钟

c# - 如何防止空格被修剪?

.net - 无法将 IIS 中的 SMTP 日志与 .NET 应用程序相关联

c# - 分配给接口(interface)数组初始化程序编译但为什么呢?

ASP.NET MVC 使用 AJAX 加载页面

c# - 使用 jQuery 在 ASP.NET 页面中查找文本框

.net - msxml6.dll 错误 '80072ee2' 操作超时

.net - 如何查找 Web 应用程序加载的 DLL?