【发布时间】:2012-12-17 07:18:30
【问题描述】:
根据上一个问题,我正在尝试从 MongoDB 数据库中执行 SqlBulkCopy,但出现错误并且找不到我应该具有的列类型:
来自数据源的
ObjectId类型的给定值无法转换为指定目标列的nvarchar类型。
我的DataTable 列DataType 是MongoDB.Bson.ObjectId。
Microsoft Sql Server 中承载此值的类型应该是什么?
我当前的代码:
string connectionString = GetDestinationConnectionString();
var mongoCollection = GetSourceConnectionString();
var queryFind = Query.And(Query.NotExists("sync"), Query.NotIn("challenge_guid", new List<MongoDB.Bson.BsonValue>() { "87558b59-73ee-4f10-add4-b9031551e395" }));
var sourceData = mongoCollection.Find(queryFind).ToList();
DataTable dt = CollectionHelper.ConvertTo<MongoAnswerDoc>(sourceData);
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open();
// Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName = "JK_RawChallengeAnswers";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
txtMsg.Text = ex.Message;
}
finally
{
// Dispose of the DataTable.
dt.Dispose();
// close connection
destinationConnection.Close();
}
}
}
【问题讨论】:
标签: c# sql-server mongodb mongodb-.net-driver