[英]Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'
I am trying to insert List of object into table using SqlBulkCopy .我正在尝试使用 SqlBulkCopy将 object 列表插入表中。 When the code hit the line当代码上线时
var columnNames = ((Dictionary<string, object>)bData[0]).Select(x => x.Key).ToList();'
I got:我有:
'Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'' error. 'Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'' error.
Could you help me?你可以帮帮我吗?
public Int64 PostBulkMessage(List<object> bData)
{
DataTable dt = new DataTable();
var columnNames = ((Dictionary<string, object>)bData[0]).Select(x => x.Key).ToList();
for (int i = 0; i < columnNames.Count(); i++)
{
dt.Columns.Add(columnNames[i]);
}
foreach (Dictionary<string, object> customer in bData)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < columnNames.Count(); i++)
{
dr[columnNames[i]] = customer[columnNames[i]];
}
dt.Rows.Add(dr);
}
if (dt.Rows.Count > 0)
{
string consString = ConfigurationManager.ConnectionStrings["ConnEntities"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name.
sqlBulkCopy.DestinationTableName = "dbo.BulkSent";
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
return 0;
}
Many thanks Stefan and Rahul Sharma for your support.非常感谢 Stefan 和 Rahul Sharma 的支持。 As advised by Stefan i created class and changed the code.根据 Stefan 的建议,我创建了 class 并更改了代码。 Now the bulk insert is working fine.现在批量插入工作正常。 Please verify the below code.请验证以下代码。
public Int64 PostBulkMessage(List<Bulk> bData)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Mobile", typeof(string));
dt.Columns.Add("MessageContent", typeof(string));
for (int i = 0; i < bData.Count(); i++)
{
DataRow dr = dt.NewRow();
dr[0] = bData[i].Id;
dr[1] = bData[i].Mobile;
dr[2] = bData[i].MessageContent;
dt.Rows.Add(dr);
}
if (dt.Rows.Count > 0)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnEntities"].ConnectionString;
if (connectionString.ToLower().StartsWith("metadata="))
{
System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(connectionString);
connectionString = efBuilder.ProviderConnectionString;
}
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "dbo.BulkMsg";
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
return 0;
}
public class Bulk
{
public string Id { get; set; }
public string Mobile { get; set; }
public string MessageContent { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.