将DataTable作为参数传递时的C#FormatException

时间:2015-06-05 12:26:21

标签: c# sql-server datatable formatexception

我正在尝试阅读Excel文件,并且我希望将记录发送到存储过程以便插入到表中,将DataTable作为Parameter。 这是我的StoredProcedure

    create PROCEDURE [dbo].[spInsertInvoice]
    @tblInvoice InvoiceType READONLY
    AS
    BEGIN
          SET NOCOUNT ON;
          INSERT INTO Excel
          SELECT Template, Cust_Name, Invoice_No, InvoiceDate FROM @tblInvoice
    END

这是c#代码:

if (FileUpload1.HasFile)
        {
            string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
            FileUpload1.PostedFile.SaveAs(path);
            OleDbConnection oleCon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";Extended Properties = Excel 12.0;");
            OleDbCommand Olecmd = new OleDbCommand("select * from [Sheet1$]", oleCon);
            OleDbDataAdapter dtap = new OleDbDataAdapter(Olecmd);
            DataSet ds = new DataSet();
            dtap.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Template", typeof(string)),
                    new DataColumn("Cust_Name", typeof(string)),
                    new DataColumn("Invoice_No", typeof(int)),
                    new DataColumn("InvoiceDate",typeof(DateTime)) });
            foreach (GridViewRow row in GridView1.Rows)
            {
                string Template = row.Cells[1].Text;
                string Cust_Name = row.Cells[2].Text;
                int Invoice_No = int.Parse(row.Cells[3].Text);
                DateTime InvoiceDate = DateTime.ParseExact(row.Cells[4].Text, "yyyyMMdd", CultureInfo.InvariantCulture);
                dt.Rows.Add(Template, Cust_Name, Invoice_No);



            }

            if (dt.Rows.Count > 0)
            {
                string consString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(consString))
                {
                    using (SqlCommand cmd = new SqlCommand("spInsertInvoice"))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@tblInvoice", dt);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            Response.Write("<script>alert('Inserted')</script>");
        }

当我运行我的代码时,我得到FormatException

  

字符串未被识别为有效的DateTime

 DateTime InvoiceDate = DateTime.ParseExact(row.Cells[3].Text, "yyyyMMdd", CultureInfo.InvariantCulture);

我做错了什么?

1 个答案:

答案 0 :(得分:1)

调用ParseExact的格式部分是您尝试解析的格式,因此如果excel中字符串的日期格式为 5-Jun-15 ,则应该正在使用

DateTime InvoiceDate = DateTime.ParseExact(row.Cells[3].Text, "d-MMM-yy", CultureInfo.InvariantCulture);