显示数据库插入的进度条

时间:2013-06-08 07:53:00

标签: c# sql-server sql-server-2008

我有一个应用程序,用户上传包含数百行的Excel文件,当他点击上传时,文件将转换为DataTable并发送到数据库以供存储过程插入。上传花费了太多时间,我想显示已插入行数的进度。反正有吗?

List<DataTable> tb = Helper.SplitTableToManyTables(FileTb, 50); // this is a function that split the Main dataTable to several Dt each have 50 rows
        int importedRowsCount = 0;
        for (int KLoop = 0; KLoop < tb.Count; KLoop++)
        {
            string[] ContctInfoCols = { "First Name", "Last Name", "Nickname", "Job Title", "Birthday", "Gender", "E-mail Address", "E-mail Address 2", "E-mail Address 3", "Web Page", "Mobile", "Mobile 2", "Mobile 3", "Home Phone", "Home Phone 2", "Business Phone", "Business Phone 2", "Other Phone", "Business Fax", "Home Fax", "Home Street", "Business Street", "Notes", "Company" };//the order of fields is very important
            string contact = "";
            DataTable resTb = new DataTable("ContactsTb");
            resTb.Columns.Add("ContactInfo");
            for (int iLoop = 0; iLoop < tb[KLoop].Rows.Count; iLoop++)
            {
                contact = ";";
                DataRow dr = resTb.NewRow();
                for (int jLoop = 0; jLoop < ContctInfoCols.Length; jLoop++)
                {
                    if (ContctInfoCols[jLoop] != "" && tb[KLoop].Rows[iLoop][ContctInfoCols[jLoop]].ToString() != "")
                        contact += (jLoop + 1).ToString() + "~" + tb[KLoop].Rows[iLoop][ContctInfoCols[jLoop]].ToString().Replace("/", "") + ";";
                }
                dr["ContactInfo"] = contact;
                resTb.Rows.Add(dr);
            }
            if (QueriesDataHelper.ImportContacts(resTb, int.Parse(TxtHiddenGroupId.Value), Session)) // here is the stored procedure call 
            {
                importedRowsCount += resTb.Rows.Count;
                var script = "DisplayProgressMsg(" + importedRowsCount + ")";
                ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", script, true); // here i am trying to display the currently inserted rows but it's not working
                if (KLoop == tb.Count - 1)
                    MessageBox.Show(importedRowsCount.ToString()+" Contacts imported successfully");
            }
            //else
            // MessageBox.Show("Sorry,an error occured during contacts upload.");
        } 

     function DisplayProgressMsg(msg) {
        alert(msg);
     }

1 个答案:

答案 0 :(得分:1)

  

上传花费了太多时间,我想显示已插入的行数的进度。反正有吗?

这取决于您如何插入数据。

  

发送到数据库以由存储过程插入。

如果您使用的是SqlDbType.Structured参数,则可能会注意到枚举参数的IEnumerable<SqlDataRecord>值的距离。

如果您每行调用一次存储过程,则计算到目前为止已经存储的过程调用次数。

如果您使用SqlBulkCopy,则可以使用SqlRowsCopied事件接收有关插入行数的反馈。

如果您使用的是SqlDataAdapter,则可以查看RowUpdatedRowUpdating个事件。

使用别的东西?阅读精细手册,了解可用的反馈选项。

虽然你可能错过了真正的问题:

  

几百行......花费太多时间

查看您如何插入行,您应该能够每秒插入数千行,这可能会使您无需提供反馈。