简体   繁体   中英

The given ColumnMapping does not match up with any column in the source or destination for huge data

I am getting error on bind grid

string path = string.Concat(Server.MapPath("~/UploadFile/" + FileUpload1.FileName));
FileUpload1.SaveAs(path); 
string excelCS = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);

using (OleDbConnection con = new OleDbConnection(excelCS))
{
    OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", con);
    con.Open();
    DataTable dt = new DataTable();
    dt.Load(cmd.ExecuteReader());
    // Create DbDataReader to Data Worksheet
    DbDataReader dr = cmd.ExecuteReader();
    // SQL Server Connection String
    string CS = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    SqlBulkCopy bulkInsert = new SqlBulkCopy(CS);

    bulkInsert.DestinationTableName = "tbl_data";
    for (int i = 0; i < dt.Columns.Count; i++)
    {
         bulkInsert.ColumnMappings.Add(i, i);
    }
    bulkInsert.WriteToServer(dr);
    BindGridView();
    con.Close();
    lblerror.Text = "Your file uploaded successfull"

EDIT:

private void BindGridView() 
{ 
    string conString = 
    ConfigurationManager.ConnectionStrings["con"].ConnectionString; 
    SqlConnection con = new SqlConnection(conString); 
    SqlCommand cmd = new SqlCommand("select * from tbl_data", con); 
    con.Open(); 
    gvUpload.DataSource = cmd.ExecuteReader(); 
    gvUpload.DataBind(); 
    con.Close(); 
}

Okay, Nits. This is an example, using your codebase, of something you could be doing. Please try it, after adapting to your specific scenario. You have checked this question out right?

static void Main(string[] args)
{
    string path = string.Concat(Server.MapPath("~/UploadFile/" + FileUpload1.FileName));
    FileUpload1.SaveAs(path);

    string excelCS = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);

    using (OleDbConnection con = new OleDbConnection(excelCS))
    {
        DataTable excelData = new DataTable();

        using (OleDbCommand excelSource = new OleDbCommand("select * from [Sheet1$]", con))
        {
            con.Open();
            excelData.Load(excelSource.ExecuteReader());
        }

        // SQL Server Connection String
        string CS = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        SqlBulkCopy bulkInsert = new SqlBulkCopy(CS);
        bulkInsert.DestinationTableName = "tbl_data";   
        bulkInsert.ColumnMappings.Add(0,0);
        bulkInsert.ColumnMappings.Add(1,1);     

        try
        {
            bulkInsert.WriteToServer(excelData);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

        con.Close();
        lblerror.Text = "Your file uploaded successfully";
    }
    BindGridView();
}

private static void BindGridView()
{
    string conString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("select ID, Mail_ID from tbl_data", con))
        {
            con.Open();
            var sqlReader = cmd.ExecuteReader();
            Console.WriteLine("START: Sql Columns");
            foreach (var sqlColumnName in Enumerable.Range(0, sqlReader.FieldCount).Select(sqlReader.GetName))
            {
                Console.WriteLine(sqlColumnName);
            }
            Console.WriteLine("END: Sql Columns");
            gvUpload.DataSource = sqlReader;
            gvUpload.DataBind();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM