我如何...计算插入的记录数。 [英] How do I...Count the no of records inserted.

查看:57
本文介绍了我如何...计算插入的记录数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在数据表中获取excel值,然后最后我正在更新/在数据库中插入记录。



每件事情都运转良好,但我需要的是一旦完成所有记录,它应该向用户显示消息,因为插入的记录没有..



并且在插入一些时间时它会抛出一个错误,因为

Hi
I'm getting the values form excel in datatable then finally i'm updating/Inserting the record in database.

Every thing is working fine but what i need is once finishing all the records it should display the message to the user as ..no of records inserted.

and also while inserting some time it is throwing an error as

String was not recognized as a valid DateTime.





例如:

已插入20条记录。



这是我的代码:



Eg:
20 records inserted.

Here is my code:

private void Import_To_Grid(string FilePath, string Extension, string isHDR)
     {
         String strConnString = ConfigurationManager.ConnectionStrings["CARGONETConnectionString"].ConnectionString;
         //file upload path
         string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);
         //file name
         string FileName = lblFileName.Text;
         //Create connection string to Excel work book

         string conStr = "";
         switch (Extension)
         {
             case ".xls": //Excel 97-03
                 conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FolderPath + FileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                 break;
             case ".xlsx": //Excel 07
                 conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FolderPath + FileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
                 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 First Sheet
          connExcel.Open();
          DataTable dtExcelSchema;
          dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
          string SheetName = ddlSheets.SelectedValue.ToString();
          connExcel.Close();

         //Read Data from First Sheet
          connExcel.Open();
          cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
          oda.SelectCommand = cmdExcel;
          oda.Fill(dt);
          connExcel.Close();

         //Bind to Database

         int count=0;
          using (LQTransAgentSeaFreightRateDataContext DB = new LQTransAgentSeaFreightRateDataContext())
          {
              foreach (DataRow r in dt.Rows)
              {
                  var newSFR = new TB_TransAgentSeaFreightRate_2
                  {
                      POD = r["POL"].ToString(),
                      POL = r["POD"].ToString(),
                      Forwarder = r["FORWARDER"].ToString(),
                      ForwarderReference = r["FORWARDER REFERENCE"].ToString(),
                      ShippingLine = r["SHIPPING LINE"].ToString(),
                      ContainerType = r["CONTAINER TYPE"].ToString(),
                      ContainerSize = r["CONTAINER SIZE"].ToString(),
                      ValidFrom = Convert.ToDateTime(r["VALIDITY FROM"].ToString()),
                      ValidTo = Convert.ToDateTime(r["VALITITY TO"].ToString()),
                      BasicRate = Convert.ToDecimal(r["BASIC RATE"]),
                      PAF = Convert.ToDecimal(r["PAF "]),
                      CAF = Convert.ToDecimal(r["CAF"]),
                      PSS = Convert.ToDecimal(r["PSS"]),
                      TotalAmount = Convert.ToDecimal(r["TOTAL AMOUNT"]),
                      FreeDays = Convert.ToDecimal(r["FREE DAYS"]),
                      CreditDays = r["CREDIT DAYS"].ToString(),
                      NITDeposit = r["NIT DEPOSIT"].ToString(),
                      tASF_NUIsActive = 1,
                      tASF_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text)

                  };
                  DB.TB_TransAgentSeaFreightRate_2s.InsertOnSubmit(newSFR);
                  DB.SubmitChanges();
                  count = count + 1;
              }
          }
          //ScriptManager.RegisterStartupScript(this, Up.GetType(), "ALERT", "alert('Saved Successfully');", true);
          //Bind Data to GridView
          dg_AgentSFR.Caption = Path.GetFileName(FilePath);
          dg_AgentSFR.DataSource = dt;
          dg_AgentSFR.DataBind();
          //savedatafromgv();

     }





请帮帮我。

谢谢提前。



please help me.
Thanks in advance.

推荐答案

DB.GetChangeSet().Inserts.Count()

应该可以计算在提交更改后使用它

你可以把那个submitChanges放在foreach之外

should work to get count use it
after Submit changes and you can put that submitChanges outside foreach


这篇关于我如何...计算插入的记录数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆