简体   繁体   中英

given ColumnMapping does not match up

I am getting the following error after table created in destination database. Getting the error on sqlBulkCopy.WriteToServer(dtCeTask);

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

using (SqlCeConnection conn = new SqlCeConnection(_connectionString))
            {
                try
                {
                    conn.Open();

                    //CETASK
                    DataTable dtCeTask = new DataTable("TempTask");
                    StringBuilder sqlTask = new StringBuilder();

                    sqlTask.Append("select  StartDate,Status,Priority,Complete, PercentComplete,TimerDuration, ItemLastModified, MatterIdentifier, MatterName, TimeEntryIdentifier, Isrecurring, Originator, createdDate, DisplayName, DisplayText ");
                    sqlTask.Append("from TaskOutlookItemProxy  ");

                    using (SqlCeDataAdapter daCeTask = new SqlCeDataAdapter(sqlTask.ToString(), conn))
                    { daCeTask.Fill(dtCeTask); }

                    using (SqlConnection sqlconnection = new SqlConnection(strConnString))
                    {
                        sqlconnection.Open();

                        // create table if not exists 
                        string createTableQuery = @"Create Table TempTask1 
                        ( StartDate datetime, Status nvarchar(255), Priority nvarchar(255), Complete bit, PercentComplete int, TimerDuration int,ItemLastModified DateTime, MatterIdentifier nvarchar(255),MatterName nvarchar(255), TimeEntryIdentifier nvarchar(255),  Isrecurring bit, Originator nvarchar(255), createdDate DateTime, DisplayName nvarchar(255), DisplayText nvarchar(255) )";
                        SqlCommand command = new SqlCommand(createTableQuery, sqlconnection);
                        command.ExecuteNonQuery();



                        // Copy the DataTable to SQL Server Table using SqlBulkCopy
                        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlconnection))
                        {
                            sqlBulkCopy.DestinationTableName = dtCeTask.TableName;

                            foreach (var column in dtCeTask.Columns)
                            {
                                sqlBulkCopy.ColumnMappings.Add(column.ToString(), column.ToString());
                            }


                            sqlBulkCopy.WriteToServer(dtCeTask);
                        }
                    }

                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }

ColumnMappings are case sensitive - this has caught me out before ( blog ) and I suspect is the case for you.

In your original query, most columns are in CamelCase....except createdDate (lower case "c"). So maybe, if your db table definition has the column as "CreatedDate", then the query will run fine, but your SqlBulkCopy will fail if it's using the incorrectly cased column name

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