本文介绍了使用ASP.NET上传Excel工作表并存储mysql数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误::
命令执行期间遇到致命错误.

Error::
Fatal error encountered during command execution.

Con.Open();
        string path = fileuploadExcel.PostedFile.FileName;
       // fileuploadExcel.SaveAs(Server.MapPath("orders.xlsx"));
        
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("orders.xlsx") + ";Extended Properties=Excel 12.0;Persist Security Info=False";
        
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        
        DataTable dt = new DataTable();
        excelConnection.Open(); 
        try
        {           

             dt.Columns.AddRange(new DataColumn[11] { new DataColumn("Date", typeof(DateTime)),
                new DataColumn("Ref_No", typeof(string)),
                new DataColumn("Partys_Name",typeof(string)),
               new DataColumn("Pending", typeof(float)),
        new DataColumn("<30 days ", typeof(float)),
        new DataColumn("30to60days", typeof(float)),
        new DataColumn("60 to 90 days", typeof(float)),
        new DataColumn("90 to 120 days", typeof(float)),
        new DataColumn("(> 120 days )", typeof(float)),
        new DataColumn("Post-Dated", typeof(string)),
        new DataColumn("Final", typeof(float))});
             OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection);
             OleDbDataReader dReader;
             dReader = cmd.ExecuteReader();

            // create a connection string with your sql database

            //insert excel data in student table
             MySqlCommand cm = new MySqlCommand("insert into tbl_upload values(@Date,@Ref_No,@Partys_Name,@Pending,@<30 days,@30to60days,@60 to 90 days,@90 to 120 days,@(> 120 days ),@Post-Dated,@Final)", Con);

            cm.Parameters.AddWithValue("Date", "Date");

            cm.Parameters.AddWithValue("Ref_No", "Ref_No");

            cmd.Parameters.AddWithValue("Partys_Name", "Partys_Name");

            cm.Parameters.AddWithValue("<30 days ", "less30days");
             cm.Parameters.AddWithValue("30to60days", "bet30to60days");
             cm.Parameters.AddWithValue("60 to 90 days", "bet60to90days");
             cm.Parameters.AddWithValue("90 to 120 days", "bet90to120days");
             cm.Parameters.AddWithValue("(> 120 days )", "greater120days");
             cm.Parameters.AddWithValue("Post-Dated", "Posted_Dated");
            cm.Parameters.AddWithValue("Final", "Final");

            int i = cm.ExecuteNonQuery();

            if (i > 0)

            {

                Label2.Text = "Data inserted successfully";

            }           

        }       

            catch (Exception ex)

        {
                Label2.Text =ex.Message;
            }
        excelConnection.Close();
        Con.Close();
    }

推荐答案



这篇关于使用ASP.NET上传Excel工作表并存储mysql数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:01