简体   繁体   English

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'

[英]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.

相关问题 无法将类型为“Newtonsoft.Json.Linq.JObject”的对象强制转换为“System.Runtime.Serialization.ISafeSerializationData” - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Runtime.Serialization.ISafeSerializationData' 无法将System.String强制转换为System.Collections.Generic.Dictionary类型 <string, string> 与会话变量 - Unable to cast System.String to type System.Collections.Generic.Dictionary<string, string> with Session variable Firebase 数据库错误:System.Collections.Generic.Dictionary`2[System.String,System.Object] - Firebase Database Error: System.Collections.Generic.Dictionary`2[System.String,System.Object] 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' System.InvalidCastException:“无法将“System.String”类型的对象转换为“Newtonsoft.Json.Linq.JToken”类型。 - System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'Newtonsoft.Json.Linq.JToken'.' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“System.Collections.Generic.IEnumerable”<Employee> &#39; - Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>' 无法将“Newtonsoft.Json.Linq.JArray”类型的对象转换为“System.Collections.Generic.List” - Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List` 无法将“OpenQA.Selenium.Remote.RemoteWebElement”类型的对象强制转换为“System.Collections.Generic.Di ctionary”2 [System.String,System.Object] - Unable to cast object of type 'OpenQA.Selenium.Remote.RemoteWebElement' to type 'System.Collections.Generic.Di ctionary`2[System.String,System.Object] Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray' - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM