我正在使用 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/