没有给出一个或多个必需参数OLEDB异常的值

时间:2014-05-29 05:31:53

标签: c#

以前我的代码工作正常,但现在却抛出了错误:

No value given for one or more  required Parameter OLEDB Exception

代码:

string excelConnectionString = string.Empty;
OleDbConnection excelConnection;
try
{
     if (Request.Files["FileUpload"].ContentLength > 0)
                {
                    string extension = System.IO.Path.GetExtension(Request.Files["FileUpload"].FileName);
                    String fileName = System.IO.Path.GetFileName(Request.Files["FileUpload"].FileName);
                    if (extension == ".xls" || extension == ".xlsx")
                    {
                        string currentPath = System.IO.Path.GetFullPath(Request.Files["FileUpload"].FileName);




                        {
                            string mappedUploadPath = string.Format("{0}\\{1}", Server.MapPath("~/Content/UploadedFolder"), fileName);
                            if (System.IO.File.Exists(mappedUploadPath))
                                System.IO.File.Delete(mappedUploadPath);

                            Request.Files["FileUpload"].SaveAs(mappedUploadPath);

                            // Create connection string to Excel work book
                            excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + mappedUploadPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"";
                            //Creating an entityconnection
                            using (EntityConnection sqlConnectionString =
                            new EntityConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
                            {

                                excelConnection = new OleDbConnection(excelConnectionString);
                                excelConnection.Open();
                                OleDbDataReader dReader;


                                //Create OleDbCommand to fetch data from Excel
                                //PromotionMaster
                                OleDbCommand cmd = new OleDbCommand("Select [SKU],[SupplierId],[Price],[Validity],[LastUpdated],[EnteredBy],[Quantity],[CrediteTermId],[Approved],[DeliveryDate],[ETA],[CommModeId] from [Sheet1$]", excelConnection);
                                dReader = cmd.ExecuteReader();
                                SqlBulkCopy sqlBulkPromotionMaster = new SqlBulkCopy(((SqlConnection)sqlConnectionString.StoreConnection).ConnectionString);

                                sqlBulkPromotionMaster.DestinationTableName = "TempPriceList";
                                sqlBulkPromotionMaster.ColumnMappings.Add("SKU", "SKU");
                                sqlBulkPromotionMaster.ColumnMappings.Add("SupplierId", "SupplierId");
                                sqlBulkPromotionMaster.ColumnMappings.Add("Price", "Price");
                                sqlBulkPromotionMaster.ColumnMappings.Add("Validity", "Validity");
                                sqlBulkPromotionMaster.ColumnMappings.Add("LastUpdated", "LastUpdated");
                                sqlBulkPromotionMaster.ColumnMappings.Add("EnteredBy", "EnteredBy");
                                sqlBulkPromotionMaster.ColumnMappings.Add("Quantity", "Quantity");
                                sqlBulkPromotionMaster.ColumnMappings.Add("CrediteTermId", "CrediteTermId");
                                sqlBulkPromotionMaster.ColumnMappings.Add("Approved", "Approved");
                                sqlBulkPromotionMaster.ColumnMappings.Add("DeliveryDate", "DeliveryDate");
                                sqlBulkPromotionMaster.ColumnMappings.Add("ETA", "ETA");
                                sqlBulkPromotionMaster.ColumnMappings.Add("CommModeId", "CommModeId");


                                sqlBulkPromotionMaster.WriteToServer(dReader);
                                TempData["alertMessage"] = "Data Stored Successfully of PromotionMaster";


                                excelConnection.Close();


                            }


                        }


                    }
                }
                return RedirectToAction("LoadPriceExcel");
            }
            catch (Exception e)
            {
                GC.Collect();
                TempData["alertMessage"] = "Exception Occured";
                return RedirectToAction("LoadPriceExcel");
            }
        }

dReader = cmd.ExecuteReader();这里抛出异常没有给出一个或多个必需参数的值。

Excel中的所有字段都与表格完全匹配。

以前它正在工作,它正在抛出错误。

1 个答案:

答案 0 :(得分:0)

检查你的第一行(A1:ZZ1)是否有表格的标题。