简体   繁体   中英

The given ColumnName 'ACTUAL DATE' does not match up with any column in data source

数据

I am importing my excel file into sql database using sqlBulkCopy but as soon as I start the import this error pops up. I am using MVC5 and entityframework 6. I have checked for the same columns in both the excel file and the sql tabel everything looks good. I have set StgtId as an identity field which I am not importing through the excel sheet. I think it would be handled by the table if I am not wrong.

   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]

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

Try using an ordinal number for the source column instead of the name. Eg

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

as supported by one of the overloads to the SqlBulkCopyColumnMappingCollection.Add() method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM