在上一个问题中,我正在尝试SqlBulkCopy
从 MongoDB 数据库中执行操作,但出现错误并且找不到我应该具有的列类型:
来自数据源的 type 的给定值
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();
}
}
}