给定的ColumnName'ACTUAL DATE'与数据源中的任何列都不匹配

时间:2019-05-10 17:22:24

标签: c# sql asp.net-mvc-5 sqlbulkcopy excel-import

Data

我正在使用sqlBulkCopy将excel文件导入到sql数据库中,但是一旦我开始导入,就会弹出此错误。我正在使用MVC5和EntityFramework6。我已经检查了excel文件和sql表格中的相同列,一切看起来都很好。我已将StgtId设置为我不通过excel表导入的标识字段。我认为,如果我没有记错的话,它将由表格处理。

   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]

2 个答案:

答案 0 :(得分:1)

尝试在名称“ [ACTUAL DATE]”周围添加方括号。

答案 1 :(得分:0)

尝试对源列使用序号而不是名称。例如

sqlBulkCopy.ColumnMappings.Add(0, "ActualDate");

overloads to the SqlBulkCopyColumnMappingCollection.Add() method之一支持。