使用带有数据表的SqlBulkCopy来插入excel记录

时间:2014-07-19 21:59:51

标签: c# asp.net c#-4.0

我正在尝试使用SqlBulkCopy将数据插入数据库

我有这段代码

 string mydemo="my demo record";
        DataTable prodSalesData = new DataTable("ProductSalesData");

        // Create Column 1: SaleDate
        DataColumn dateColumn = new DataColumn();
        dateColumn.DataType = Type.GetType("System.String");
        dateColumn.ColumnName = "SaleDate";

        prodSalesData.Columns.Add(dateColumn);


        DataRow dailyProductSalesRow = prodSalesData.NewRow();
        dailyProductSalesRow["SaleDate"] = mydemo;


        // Create DbDataReader to Data Worksheet
        using (OleDbDataReader dr1 = command1.ExecuteReader())
        {
            // Bulk Copy to SQL Server
            using (SqlBulkCopy bulkCopy1 = new SqlBulkCopy(con))
            {

                bulkCopy1.DestinationTableName = "activity1";
                bulkCopy1.ColumnMappings.Add(0, "id");
                bulkCopy1.ColumnMappings.Add(1,"name");
                bulkCopy1.ColumnMappings.Add(2, "activity1first");
                bulkCopy1.ColumnMappings.Add(3, "activity1second");
                bulkCopy1.ColumnMappings.Add(4, prodSalesData.Columns.ToString());
                bulkCopy1.WriteToServer(dr1);

            }
        }

这里有四个recods来自xcell文件,但我想在同一个表中插入一个外部数据,我试了这么多,但它给了我这个错误

The given ColumnMapping does not match up with any column in the source or destination.

任何帮助?

由于

0 个答案:

没有答案