gpt4 book ai didi

c# - SQL 批量复制 "The given value of type String from the data source cannot be converted to type datetime of the specified target column"使用 ASP.NET

转载 作者:太空狗 更新时间:2023-10-29 23:58:10 24 4
gpt4 key购买 nike

我正在开发 ASP.NET MVC4 项目,我正在尝试使用 SQL 批量复制将数据从 xlsx 文件(Excel 2010 文件)导出到我的数据库。我的 Excel 文件只包含两列:第一列包含数字(从 1 到 25),第二列包含字符(连续的“a、b、c”系列)

为了导出数据,我尝试这样做,但出现错误“数据源中 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");
}

知道导致此错误的原因吗?

最佳答案

SqlBulkCopy.WriteToServer(DataTable) 如果 DataTable 的列顺序与数据库中表定义的列顺序不同(当这导致类型或长度不兼容)。显然 WriteToServer 方法不映射列名。

关于c# - SQL 批量复制 "The given value of type String from the data source cannot be converted to type datetime of the specified target column"使用 ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com