C# SqlBulkCopy列映射不工作

C# SqlBulkCopy列映射不工作,c#,asp.net,sql-server,sqlbulkcopy,C#,Asp.net,Sql Server,Sqlbulkcopy,可能重复: 我有一张excel表格,上面有列: excelid | exceldata 1 | cat 2 | bat 3 | rat 我的数据库表: id (int) | varchar(50) 以下是我的代码: protected void Button1_Click(object sender, EventArgs e) { string strFileType = System.IO.Path.GetExtension(FileUploa

可能重复:

我有一张excel表格,上面有列:

excelid | exceldata
1       | cat
2       | bat
3       | rat
我的数据库表:

id (int) | varchar(50)
以下是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
    string strFileName = FileUpload1.PostedFile.FileName.ToString();

    FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
    string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);

    string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+strNewPath +"; Extended Properties=Excel 8.0;");

    //string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\myFolder\\Book1.xls;" + "Extended Properties=Excel 8.0;"); 


        // Create Connection to Excel Workbook
        using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
        {
            OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);

            connection.Open();

            // Create DbDataReader to Data Worksheet
            using (DbDataReader dr = command.ExecuteReader())
            {
                // SQL Server Connection String
                string sqlConnectionString = "Data Source=ARBAAZ-1B14C081;Initial Catalog=abc;Integrated Security=True";

                con.Open();
                DataTable dt1 = new DataTable();
                string s = "select count(*) from ExcelTable"; 
                string r = ""; 
                SqlCommand cmd1 = new SqlCommand(s, con);
                try 
                { 
                    SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
                    da1.Fill(dt1);
                }
                catch { } 
                int RecordCount; 
                RecordCount = Convert.ToInt32(cmd1.ExecuteScalar());
                r = RecordCount.ToString(); 
                Label1.Text = r;
                con.Close();
                int prv = Convert.ToInt32(r);





                // Bulk Copy to SQL Server
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                {



                    bulkCopy.DestinationTableName = "ExcelTable";


                    SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("id", "id");
                    SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("data", "data");

                    bulkCopy.ColumnMappings.Add(mapping1);
                    bulkCopy.ColumnMappings.Add(mapping2);


                    bulkCopy.WriteToServer(dr);

                }
                con.Open();



                DataTable dt = new DataTable(); 
                s = "select count(*) from ExcelTable";  r = ""; 
                SqlCommand cmd = new SqlCommand(s, con); 
                try { SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt); 
                }
                catch { } 
                RecordCount = Convert.ToInt32(cmd.ExecuteScalar()); 
                r = RecordCount.ToString(); Label1.Text = r;
                con.Close();
                int ltr = Convert.ToInt32(r);
                if (prv == ltr)
                {
                    Label1.Text = "No records Added";
                }
                else
                {
                    Label1.Text = "Records Added Successfully !";
                }

            }
        }

    }
//protected void Button2_Click(object sender, EventArgs e)
//{
//    string fileName = FileUpload1.PostedFile.FileName.ToString();
//    Response.Write(fileName);
//}
protected void Button2_Click(object sender, EventArgs e)
{

}

您的excel电子表格列包括:

excelid  |  exceldata
您的db列是(我猜您实际上并没有说):

因此,您的映射应该是

SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("excelid", "id");
SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("exceldata", "data");

我上面发布的代码不起作用,我的意思是列映射似乎不起作用。如果excel和数据库中的列名称不同(即使在使用列映射后),我无法上载excel工作表。您看到了什么异常?我尝试了(“excelid”、“id”)和(“id”、“excelid”),但两者都不起作用!
SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("excelid", "id");
SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("exceldata", "data");