给定的 ColumnName 'ACTUAL DATE' 与数据源中的任何列都不匹配
The given ColumnName 'ACTUAL DATE' does not match up with any column in data source
我正在使用 sqlBulkCopy 将我的 excel 文件导入到 sql 数据库中,但是一旦我开始导入,就会弹出此错误。我正在使用 MVC5 和 entityframework 6。我检查了 excel 文件和 sql 表格中的相同列,一切看起来都不错。我已将 StgtId 设置为身份字段,我不会通过 excel sheet 导入它。如果我没记错的话,我想 table 会处理的。
public ActionResult Structure(HttpPostedFileBase postedFile)
{
string filePath = string.Empty;
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
string conString = string.Empty;
switch (extension)
{
case ".xls": //Excel 97-03.
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07 and above.
conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
DataTable dt = new DataTable();
conString = string.Format(conString, filePath);
using (OleDbConnection connExcel = new OleDbConnection(conString))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//Get the name of First Sheet.
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
conString = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name.
sqlBulkCopy.DestinationTableName = "dbo.bomStructuredImportTgt";
//[OPTIONAL]: Map the Excel columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("ACTUAL DATE", "ActualDate");
sqlBulkCopy.ColumnMappings.Add("Description", "Description");
sqlBulkCopy.ColumnMappings.Add("Level", "Level");
sqlBulkCopy.ColumnMappings.Add("PARENT_PARTNO", "ParentPartNumber");
sqlBulkCopy.ColumnMappings.Add("PART_NO", "PartNumber");
sqlBulkCopy.ColumnMappings.Add("PART_NAME", "PartName");
sqlBulkCopy.ColumnMappings.Add("HNS", "HNS");
sqlBulkCopy.ColumnMappings.Add("DWGSZ", "DWGSZ");
sqlBulkCopy.ColumnMappings.Add("PART", "Part");
sqlBulkCopy.ColumnMappings.Add("L1QTY", "L1Quantity");
sqlBulkCopy.ColumnMappings.Add("COLORM", "ColorM");
sqlBulkCopy.ColumnMappings.Add("ATTCD", "ATTCD");
sqlBulkCopy.ColumnMappings.Add("KD", "KD");
sqlBulkCopy.ColumnMappings.Add("SELL", "Sell");
sqlBulkCopy.ColumnMappings.Add("PL_GROUP", "PlGroup");
sqlBulkCopy.ColumnMappings.Add("PL1", "PL1");
sqlBulkCopy.ColumnMappings.Add("AT1", "AT1");
sqlBulkCopy.ColumnMappings.Add("PL2", "PL2");
sqlBulkCopy.ColumnMappings.Add("AT2", "AT2");
sqlBulkCopy.ColumnMappings.Add("PL3", "PL3");
sqlBulkCopy.ColumnMappings.Add("PLANT", "Plant");
sqlBulkCopy.ColumnMappings.Add("SHRPCMINMAX", "SHRPCMINMAX");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
return View();
}[I have entered a screenshot of all the fields in my table.][1]
尝试在名称“[实际日期]”两边添加方括号。
尝试对源列使用序号而不是名称。例如
sqlBulkCopy.ColumnMappings.Add(0, "ActualDate");
得到 overloads to the SqlBulkCopyColumnMappingCollection.Add() method 之一的支持。
我正在使用 sqlBulkCopy 将我的 excel 文件导入到 sql 数据库中,但是一旦我开始导入,就会弹出此错误。我正在使用 MVC5 和 entityframework 6。我检查了 excel 文件和 sql 表格中的相同列,一切看起来都不错。我已将 StgtId 设置为身份字段,我不会通过 excel sheet 导入它。如果我没记错的话,我想 table 会处理的。
public ActionResult Structure(HttpPostedFileBase postedFile)
{
string filePath = string.Empty;
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
string conString = string.Empty;
switch (extension)
{
case ".xls": //Excel 97-03.
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07 and above.
conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
DataTable dt = new DataTable();
conString = string.Format(conString, filePath);
using (OleDbConnection connExcel = new OleDbConnection(conString))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//Get the name of First Sheet.
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
conString = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name.
sqlBulkCopy.DestinationTableName = "dbo.bomStructuredImportTgt";
//[OPTIONAL]: Map the Excel columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("ACTUAL DATE", "ActualDate");
sqlBulkCopy.ColumnMappings.Add("Description", "Description");
sqlBulkCopy.ColumnMappings.Add("Level", "Level");
sqlBulkCopy.ColumnMappings.Add("PARENT_PARTNO", "ParentPartNumber");
sqlBulkCopy.ColumnMappings.Add("PART_NO", "PartNumber");
sqlBulkCopy.ColumnMappings.Add("PART_NAME", "PartName");
sqlBulkCopy.ColumnMappings.Add("HNS", "HNS");
sqlBulkCopy.ColumnMappings.Add("DWGSZ", "DWGSZ");
sqlBulkCopy.ColumnMappings.Add("PART", "Part");
sqlBulkCopy.ColumnMappings.Add("L1QTY", "L1Quantity");
sqlBulkCopy.ColumnMappings.Add("COLORM", "ColorM");
sqlBulkCopy.ColumnMappings.Add("ATTCD", "ATTCD");
sqlBulkCopy.ColumnMappings.Add("KD", "KD");
sqlBulkCopy.ColumnMappings.Add("SELL", "Sell");
sqlBulkCopy.ColumnMappings.Add("PL_GROUP", "PlGroup");
sqlBulkCopy.ColumnMappings.Add("PL1", "PL1");
sqlBulkCopy.ColumnMappings.Add("AT1", "AT1");
sqlBulkCopy.ColumnMappings.Add("PL2", "PL2");
sqlBulkCopy.ColumnMappings.Add("AT2", "AT2");
sqlBulkCopy.ColumnMappings.Add("PL3", "PL3");
sqlBulkCopy.ColumnMappings.Add("PLANT", "Plant");
sqlBulkCopy.ColumnMappings.Add("SHRPCMINMAX", "SHRPCMINMAX");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
return View();
}[I have entered a screenshot of all the fields in my table.][1]
尝试在名称“[实际日期]”两边添加方括号。
尝试对源列使用序号而不是名称。例如
sqlBulkCopy.ColumnMappings.Add(0, "ActualDate");
得到 overloads to the SqlBulkCopyColumnMappingCollection.Add() method 之一的支持。