简体   繁体   English

SQL批量复制“使用ASP.NET无法将数据源中String类型的给定值转换为指定目标列的类型日期时间”

[英]SQL Bulk Copy “The given value of type String from the data source cannot be converted to type datetime of the specified target column” using ASP.NET

I'm working on an ASP.NET MVC4 projet and I'm trying to export data from an xlsx file (Excel 2010 file) to my database by using SQL Bulk Copy. 我正在研究ASP.NET MVC4项目,我正在尝试使用SQL批量复制将数据从xlsx文件(Excel 2010文件)导出到我的数据库。 My Excel file contains only 2 columns : the first contains numbers (from 1 to 25) and the second contains characters (successive series of "a, b, c") 我的Excel文件只包含2列:第一列包含数字(从1到25),第二列包含字符(连续系列“a,b,c”)

This is how I try to do in order to export data but I got the error "The given value of type String from the data source cannot be converted to type int of the specified target column" : 这是我尝试导出数据的方法,但是我收到错误“数据源中String类型的给定值无法转换为指定目标列的int类型”

public ActionResult Bulk()
{    
    string xConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\maarab\Documents\BulkCopy.xlsx;Extended Properties=Excel 12.0;";
    using (OleDbConnection connection = new OleDbConnection(xConnStr))
    {
        OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);
        connection.Open();    
        string dbConnection = ((EntityConnection)db.Connection).StoreConnection.ConnectionString;

        // Create DbDataReader to Data Worksheet
        using (DbDataReader dr = command.ExecuteReader())
        {
            using (var bulkCopy = new SqlBulkCopy(dbConnection))
            {    
                bulkCopy.DestinationTableName = "bm_test" 
                bulkCopy.WriteToServer(dr); //here I got the error    
            }
        }

        return RedirectToAction("Index");
}

Any idea about what's causing this error? 是什么导致这个错误?

SqlBulkCopy.WriteToServer(DataTable) fails with confusing messages if the column order of the DataTable differs from the column order of the table definition in your database (when this causes a type or length incompatibility). 如果DataTable的列顺序与数据库中表定义的列顺序不同(当这会导致类型或长度不兼容时),则SqlBulkCopy.WriteToServer(DataTable)因混淆消息而失败。 Apparently the WriteToServer method does not map column names. 显然, WriteToServer方法不映射列名。

Mine was whining about this until I set the orders manually like so: 我一直在抱怨这个,直到我手动设置订单,如下所示:

SqlBulkCopy sbc = new SqlBulkCopy(ConnectionString, SqlBulkCopyOptions.UseInternalTransaction);
sbc.DestinationTableName = "agSoilShapes";
sbc.ColumnMappings.Add("Mukey", "Mukey");
sbc.ColumnMappings.Add("Musym", "Musym");
sbc.ColumnMappings.Add("Shapes", "Shapes");

DataTable dt = new DataTable();
dt.Columns.Add("Mukey", typeof(SqlInt32));
dt.Columns.Add("Musym", typeof(SqlString));
dt.Columns.Add("Shapes", typeof(SqlGeometry));

Thanks to others (@subsci) for comments leading me in this direction :) 感谢其他人(@subsci)发表评论,引导我走向这个方向:)

I have received a similar error when using EntityFramework.BulkInsert package. 我在使用EntityFramework.BulkInsert包时收到了类似的错误。 In this case the underlying cause was an order mismatch between database table columns and generated model metadata columns (database-first). 在这种情况下,根本原因是数据库表列和生成的模型元数据列(数据库优先)之间的顺序不匹配。

DataTable Columns order and Database tables' columns order should be the same. DataTable Columns顺序和Database表的列顺序应该相同。 It worked after updating order of tables columns. 它更新了表列的顺序后起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQlBulkCopy 无法将数据源中 DateTime 类型的给定值转换为指定目标列的 int 类型 - SQlBulkCopy The given value of type DateTime from the data source cannot be converted to type int of the specified target column 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型 - The given value of type String from the data source cannot be converted to type nvarchar of the specified target column 来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型 - The given value of type String from the data source cannot be converted to type varbinary of the specified target column 数据源中String类型的给定值无法转换为指定目标列的int类型 - The given value of type String from the data source cannot be converted to type int of the specified target column SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.' SqlBulkCopy引发异常:无法将数据源中String类型的给定值转换为指定目标列的bigint类型 - SqlBulkCopy throws exception: The given value of type String from the data source cannot be converted to type bigint of the specified target column ASP.NET FormView:“类型'System.Int32'的对象不能转换为类型'System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” 在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型 - Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM