在目标数据库中创建表后,我收到以下错误。在sqlBulkCopy.WriteToServer(dtCeTask)上获取错误;
错误:给定的ColumnMapping与源或目标中的任何列都不匹配。
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);
}
}
答案 0 :(得分:0)
ColumnMappings区分大小写 - 这在我之前发现了(blog),我怀疑是你的情况。
在原始查询中,大多数列都在CamelCase中....除了createdDate(小写" c")。所以,如果你的db表定义的列为" CreatedDate",那么查询将运行正常,但是如果你的SqlBulkCopy使用了错误的列名列,那么它将失败