c# - 如何从 json 结构批量更新 dapper?

标签 c# arrays json dapper bulkupdate

嗨,我的 JSON 结构数据如下

{
  "Imei": 356980051541947,
  "sendIds": [
    {
      "Id": 13014
    },
    {
      "Id": 13190
    },
    {
      "Id": 13419
    },
    {
      "Id": 13422
    }
  ],
  "ApplicationVersion": 68,
  "GoogleId": "asdsadazxcjkh218",
  "ImagesVersion": "123:1"
}

我的类(class)是:

public class PostData
{
    public int ApplicationVersion;
    public long Imei;
    public string GoogleId;
    public string FromDate;
    public string ToDate;
    public string ImagesVersion;
    public ItemId[] SendIds;
}

public class ItemId
{
    public int Id;
}

C# 代码简洁更新:

const string sql = "UPDATE dbo.SentMessage SET IsDelivered=1 WHERE SendID=@Id";
dapperConn.Execute(sql, postData.SendIds);

但执行时出现如下错误

Must declare the scalar variable \"@Id\

请帮助我。

最佳答案

发生此异常是因为类中的变量是 Field 而不是 Property。因此,您必须将其更改为如下代码所示的属性:

public class PostData
{
    public int ApplicationVersion { get; set; };
    public long Imei { get; set; };
    public string GoogleId { get; set; };
    public string FromDate { get; set; };
    public string ToDate { get; set; };
    public string ImagesVersion { get; set; };
    public ItemId[] SendIds { get; set; };
}

public class ItemId
{
    public int Id { get; set; };
}

关于c# - 如何从 json 结构批量更新 dapper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41032726/

相关文章:

c# - 在 C# 中解析日志文件的最佳方法

c# - VS2008 - 从 Web 服务反序列化时出错

java.util.ConcurrentModificationException 与迭代器(字符移动)

java - addValueEventListener后ArrayList变为null

jquery - 返回的 JSON 是函数调用的结构。不知道如何用 jQuery 解析

c# - 检查节点是否存在于 XDocument 中

c# - 我的 Controller 方法是异步执行的吗?

java - 将 ArrayList<String> 转换为 String[]

java - 如何评估沙盒中的用户表达式

javascript - 检查json请求的响应内容?