无法将大文件发布到数据库表的慢速数据表

里克·孔茨

我正在制作一个简单的 windows 表单站点,可以在其中上传文件,然后在可以查看的 gridview 中显示,然后可以将文件插入到数据库表中。这是我以前没有尝试过的新东西。我最初尝试使用 gridview 插入文件,显示速度很快,但我只能插入分页,然后它只会插入显示的前 10 个。我试图关闭插入的分页,但我只是在插入时得到这个:HTTP 错误 404.13 - 未找到请求过滤模块被配置为拒绝超过请求内容长度的请求

excel 文件大约有 60,000 条记录。我尝试切换到数据表进行插入,但一切都变慢了。我将此添加到网络配置中,但仍然没有运气。我尝试了一个只有 13 条记录的文件,当它加载到表中时,它最初加载似乎仍然很慢,并且它填充的页面多于数据。理想情况下,gridview 中的 2 页继续显示超过数据的 10+ 个空白页。所以我想知道减速是否与此有关。下面是我的代码。原来使用gridview的方式被注释掉了。这里有什么我做错或可以改进的地方吗?任何帮助,将不胜感激。谢谢!

protected void LoadFile(object sender, EventArgs e)
{
    if(RehireFormUpload.HasFile)
    {
        string FileName = Path.GetFileName(RehireFormUpload.PostedFile.FileName);
        string Extension = Path.GetExtension(RehireFormUpload.PostedFile.FileName);
        string FolderPath = ConfigurationManager.AppSettings["FolderPath"];

        string FilePath = Server.MapPath(FolderPath + FileName);

        RehireFormUpload.SaveAs(FilePath);
        Import_To_Grid(FilePath, Extension, "Yes");
        mainPanel.Update();
    }
}

private void Import_To_Grid(string FilePath, string Extension, string isHDR)
{
    string conStr = "";

    switch (Extension)
    {
        case ".xls": //Excel 97-03
            conStr = ConfigurationManager.ConnectionStrings["Excel03Constring"].ConnectionString;
            break;
        case ".xlsx": //Excel 07 and on
            conStr = ConfigurationManager.ConnectionStrings["Excel07Constring"].ConnectionString;
            break;
    }

    conStr = String.Format(conStr, FilePath, isHDR);
    OleDbConnection connExcel = new OleDbConnection(conStr);
    OleDbCommand cmdExcel = new OleDbCommand();
    OleDbDataAdapter oda = new OleDbDataAdapter();
    DataTable dt = new DataTable();
    cmdExcel.Connection = connExcel;

    //Get the name of the first sheet
    connExcel.Open();
    DataTable dtExcelSchema;
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
    connExcel.Close();
    //Read the data from first sheet
    connExcel.Open();
    cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
    oda.SelectCommand = cmdExcel;
    oda.Fill(dt);
    connExcel.Close();
    //dt.Rows.Remove(dt.Rows[0]);
    //dt.Rows.Remove(dt.Rows[0]);
    DataTable dtCloned = dt.Clone();
    dtCloned.Columns[0].DataType = typeof(String);
    foreach (DataRow row in dt.Rows)
    {
        dtCloned.ImportRow(row);
    }

    //Bind Data to gridview
    GridView1.Caption = Path.GetFileName(FilePath);
    GridView1.DataSource = dtCloned;
    ViewState["DataTable"] = dtCloned;
    GridView1.DataBind();

}
protected void pageIndexChanging(object sender, GridViewPageEventArgs e)
{
    string folderPath = ConfigurationManager.AppSettings["FolderPath"];
    string fileName = GridView1.Caption;
    string Extension = Path.GetExtension(fileName);
    string FilePath = Server.MapPath(folderPath + fileName);

    Import_To_Grid(FilePath, Extension, "Yes");
    GridView1.PageIndex = e.NewPageIndex;
    DataTable dt = (DataTable)ViewState["DataTable"];
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
protected void Submit(object sender, EventArgs e)
{   
    string employeeID;
    string firstName;
    string lastName;
    string termCode;
    string dateofBirth;
    string eligibleForRehire;
    string ssn;
    string terminationDate;

    SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);

    //BindingSource bs = (BindingSource)GridView1.DataSource;
    //DataTable bdt = (DataTable)bs.DataSource;
    DataTable thisData = (DataTable)ViewState["DataTable"];
    foreach (DataRow row in thisData.Rows)
    {
        employeeID = row[0].ToString(); //Convert.ToInt32(row.Cells[0].ToString());
        if (employeeID != " " && employeeID != "" && !SearchRecord(employeeID))
        {
            firstName = row[1].ToString();
            lastName = row[2].ToString();
            termCode = row[3].ToString();
            dateofBirth = row[4].ToString().Substring(0, 8);
            eligibleForRehire = row[5].ToString();
            ssn = row[6].ToString();
            terminationDate = row[7].ToString();

            InsertFile(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, ssn, terminationDate);
        }
        else if (employeeID != " " && employeeID != "" && SearchRecord(employeeID))
        {
            firstName = row[1].ToString();
            lastName = row[2].ToString();
            termCode = row[3].ToString();
            dateofBirth = row[4].ToString().Substring(0, 8);
            eligibleForRehire = row[5].ToString();
            ssn = row[6].ToString();
            terminationDate = row[7].ToString();

            UpdateRecord(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, terminationDate);
        }
        else
        {
            //Create Message Upload done
            //MessageBox.Show("End of Records");
        }
    }

    //foreach (GridViewRow row in GridView1.Rows)
    //{
    //    employeeID = row.Cells[0].Text.ToString(); //Convert.ToInt32(row.Cells[0].ToString());
    //    if (employeeID != " " && !SearchRecord(employeeID))
    //    {
    //        firstName = row.Cells[1].Text.ToString();
    //        lastName = row.Cells[2].Text.ToString();
    //        termCode = row.Cells[3].Text.ToString();
    //        dateofBirth = row.Cells[4].Text.Substring(0, 8).ToString();
    //        eligibleForRehire = row.Cells[5].Text.ToString();
    //        ssn = row.Cells[6].Text.ToString();
    //        terminationDate = row.Cells[7].Text.ToString();

    //        InsertFile(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, ssn, terminationDate);
    //    }
    //    else if (employeeID != " " && SearchRecord(employeeID))
    //    {
    //        firstName = row.Cells[1].Text.ToString();
    //        lastName = row.Cells[2].Text.ToString();
    //        termCode = row.Cells[3].Text.ToString();
    //        dateofBirth = row.Cells[4].Text.Substring(0, 8).ToString();
    //        eligibleForRehire = row.Cells[5].Text.ToString();
    //        ssn = row.Cells[6].Text.ToString();
    //        terminationDate = row.Cells[7].Text.ToString();

    //        UpdateRecord(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, terminationDate);
    //    }
    //    else
    //    {
    //        //Empty Column

    //    }
    //}

    MessageBox.Show("Record Insert Complete");

}

private void InsertFile(string employeeID, string firstName, string lastName, string termCode, string dateofBirth, string eligibleForRehire, string ssn, string terminationDate)
{

    SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
    DateTime InsertedDate = DateTime.Now;
    conn1.Open();
    String query = "INSERT INTO [EMPLOYEE_REHIRES](EmployeeID, FirstName, LastName, TermCode, DateofBirth, EligibleforRehire, SSN, TerminationDate, INSERTED_DATE) VALUES ('" + employeeID + "','" + firstName + "', '" + lastName + "', '" + termCode + "', '" + dateofBirth + "', '" + eligibleForRehire + "' , '" + ssn + "' , '" + terminationDate + "', '" + InsertedDate + "');";
    SqlCommand newCommand = new SqlCommand(query, conn1);
    newCommand.ExecuteNonQuery();
    conn1.Close();
}

private Boolean SearchRecord(string employeeID)
{
    SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
    Boolean employeeInserted = true;
    conn1.Open();

    SqlCommand newCommand2 = new SqlCommand();
    String isEmployeeInserted = "Select * FROM [EMPLOYEE_REHIRES] with (nolock) WHERE EmployeeID=" + employeeID;
    newCommand2 = new SqlCommand(isEmployeeInserted, conn1);
    SqlDataReader reader1 = newCommand2.ExecuteReader();
    string[] employees = new string[1];

    while (reader1.Read())
    {
        employees[0] = reader1[0].ToString();
    }

    conn1.Close();

    if (employees[0] == null)
    {
        employeeInserted = false;

    }

    return employeeInserted;
}

private void UpdateRecord(string employeeID, string firstName, string lastName, string termCode, string dateofBirth, string eligibleForRehire, string terminationDate)
{
    SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
    conn1.Open();
    DateTime lastUpdatedDate = DateTime.Now;
    String query = "UPDATE [EMPLOYEE_REHIRES] SET FirstName='" + firstName + "', LastName='" + lastName + "', TermCode='" + termCode + "', DateofBirth='" + dateofBirth + "', EligibleforRehire='" + eligibleForRehire + "', TerminationDate='" + terminationDate + "', LAST_UPDATED_DATE='" + lastUpdatedDate + "' WHERE EmployeeID='" + employeeID + "';";
    SqlCommand newCommand = new SqlCommand(query, conn1);
    newCommand.ExecuteNonQuery();
    conn1.Close();
}
里克·孔茨

因此,为此我最终在我的页面索引更改中添加了 Session 并解决了我的速度问题,对于我的插入,我将所有内容更改为 SQLBulkCopy 并将其加载到临时表中。然后我创建了一个存储过程来从舞台插入或更新生产表。

DataTable dt = (DataTable)Session["employeeData"];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章