C# 如何…计算插入的记录数。

C# 如何…计算插入的记录数。,c#,linq,C#,Linq,我在datatable中获取excel中的值,然后在数据库中更新/插入记录 每件事都很好,但我需要的是一旦完成所有记录,它应该向用户显示消息为..插入的记录数 而且,当插入一些时间时,它也会抛出一个错误 String was not recognized as a valid DateTime. 请帮帮我。 提前谢谢 dt.Rows.Count()将告诉数据集中有多少行可用。.确定插入的记录总数将很有帮助我猜它在ValidFrom=Convert.ToDateTime(r[“VALIDITY F

我在datatable中获取excel中的值,然后在数据库中更新/插入记录

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

而且,当插入一些时间时,它也会抛出一个错误 String was not recognized as a valid DateTime. 请帮帮我。
提前谢谢

dt.Rows.Count()将告诉数据集中有多少行可用。.确定插入的记录总数将很有帮助

我猜它在
ValidFrom=Convert.ToDateTime(r[“VALIDITY FROM”].ToString()处抛出一个错误
有效期至
。请在其崩溃时提供as值。您可能应该使用try parse。字符串未被识别为有效的日期时间此消息可能是因为输入excel文件中的日期格式不正确。请在适当的情况下尝试使用DateTime.parseexact。是的,这正是它抛出错误的地方r.只有在完成插入所有记录后才会抛出错误。:(@LakshmiNarayanan好的,谢谢你的建议,我会尝试……有人能告诉我如何计算插入的记录总数吗?
 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();

       }