"The ConnectionString property has not been initialized." SQL 批量复制上传错误

"The ConnectionString property has not been initialized." Error in SQL bulkcopy upload

我刚刚创建了 mvc 4 应用程序,在那个应用程序中我有上传 Excel 文件到数据库的功能。

这在本地主机上运行良好。

但是当我在 IIS 中部署并尝试上传时 excel file.I 出现以下错误

The ConnectionString property has not been initialized.

这些是我用来上传 excel 个文件的连接字符串

<add name="dbconnection" connectionString="Data Source="000.000.00.00";Initial Catalog=AFFHEC_DB;Persist Security Info=True;User ID=**;Password=****" providerName="System.Data.SqlClient" />
<add name="constr" connectionString="data source=000.000.0.000;Initial catalog=AFFHEC_DB;user id=**;password=*****;" />
<add name="Excel07+ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES';" />
<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'" />

这是SQL批量复制上传

的代码
    [HttpPost]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Student(HttpPostedFileBase FileUpload1, tbl_hec_Programme programme)
    {

        try
        {
            string conString = string.Empty;
            //Upload and save the file


           if (Request.Files["FileUpload1"].ContentLength > 1)
            {
                try
                {
                    string excelPath = Path.Combine(HttpContext.Server.MapPath("~/Content/"), Path.GetFileName(FileUpload1.FileName));
                    FileUpload1.SaveAs(excelPath);


                    string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
                                            conString = string.Format(conString, excelPath);  
                }
                catch (Exception ex)
                {

                    this.SetNotification("The file type submitted is invalid", NotificationEnumeration.Error);

                }

                try
                {

                   using (OleDbConnection excel_con = new OleDbConnection(conString))
                    {
                        excel_con.Open();

                        DataTable dtExcelData = new DataTable();

                        string query =    "SELECT " +
                                          "s1.HEC_ID, " +
                                          "s1.Student_Personal_ID, " +
                                          "s1.Student_Passport_Number, " +
                                          "s1.Student_ID_by_University, " +
                                          "s1.First_Name, " +


                         //    // join three tables
                        "FROM (((([Personal_Data$] as s1) " +
                        "LEFT OUTER JOIN [Enrolment_Data$] as s2 ON s1.HEC_ID = s2.HEC_ID) " +

                        using (OleDbDataAdapter oda = new OleDbDataAdapter(query, excel_con))
                        {
                            oda.Fill(dtExcelData);
                        }

                        string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                        using (SqlConnection con = new SqlConnection(consString))
                        {
                            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con,
                                                        SqlBulkCopyOptions.CheckConstraints |
                                                        SqlBulkCopyOptions.FireTriggers |
                                                        SqlBulkCopyOptions.KeepNulls |
                                                        SqlBulkCopyOptions.TableLock |
                                                        SqlBulkCopyOptions.UseInternalTransaction |
                                                        SqlBulkCopyOptions.KeepIdentity,
                                                        null))
                            {
                                //Set the database table name
                                sqlBulkCopy.DestinationTableName = "tbl_HEI_student";

                                sqlBulkCopy.BulkCopyTimeout = 0;

                                sqlBulkCopy.ColumnMappings.Add("ID", "ID");
                                sqlBulkCopy.ColumnMappings.Add("Student_Personal_ID", "Student_Personal_ID_CPR");
                                sqlBulkCopy.ColumnMappings.Add("Student_Passport_Number", "Student_Passport_Number");
                                ................


                                con.Open();

                                try
                                {

                                    sqlBulkCopy.WriteToServer(dtExcelData);

                                    int totalRowsAdded = dtExcelData.Rows.Count;


                                    UploadStatusWriter(User.Identity.GetUserId(), Status_type, Date_type);



                                    this.SetNotification("Student data successfully uploaded", NotificationEnumeration.Success);



                                    return RedirectToAction("StudentIndex", "HEI");





                                }
                                catch (Exception ex)
                                {
                                    this.SetNotification(ex.Message, NotificationEnumeration.Error);
                                    return RedirectToAction("Student", "Excel");
                                }
                                finally
                                {

                                    con.Close();


                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.SetNotification(ex.Message, NotificationEnumeration.Error);
                    return RedirectToAction("Student", "Excel");
                }

            }

            else
            {
                this.SetNotification("Please select a file first and then click the submit button", NotificationEnumeration.Error);
                return RedirectToAction("Student", "Excel");

            }



        }
        catch (Exception ex)
        {
            return RedirectToAction("Student", "Excel");
        }

    }

我遇到了同样的问题。经过多次研究,我发现我们应该在 IIS 部署中进行一些配置以及允许发布文件夹的权限。

这里是那些配置

步骤 1

  1. 添加正确的网站名称

  2. "Content Directory" 选择正确的发布文件夹路径

  3. 对于绑定,"Type" 给 httpshttp ,如果你在你的机器上部署对于 IP 地址,请提供您的机器 IP 地址,否则请提供服务器 IP 地址,并找到合适的端口。

  4. 对于应用程序池,暂时保持 DefaultAppPool

步骤 2

然后转到应用程序池找到您网站的应用程序池并将其更改为ASP.NET v4.0(如果您的.NET Framework 4.0)

步骤 3

转到您的 Web 应用程序主面板,找到目录浏览(在 IIS 部分下)

步骤 4

双击目录浏览并启用

步骤 5

在主面板(在 IIS 部分下)的应用程序中找到 ASP

步骤 6

右键单击 ASP 找到 启用父路径(在行为下)。 然后将其设为 True

步骤 7

然后转到 IIS 管理器工具的主面板,在站点下找到已部署的 site.Then 右键单击​​它。查找浏览转到该发布文件夹的位置

步骤 8

右键单击该文件夹转到属性,然后授予相关用户权限

经过以上更改后我的问题解决了希望你的也一样。

文件退出

我也遇到了同样的问题。在我后面的代码中,有两种情况仅适用于文件扩展名,.xls 和 .xlsx。请尝试添加 .XLS 和 .XLSX

简而言之,只需在这两个上加上两个大写