简体   繁体   English

使用Dapper通过ODBC使用对象更新Informix数据库

[英]Updating an Informix database over ODBC with an object using Dapper

I'm trying to figure out if there's a way to update an object in one shot with Dapper rather than having to write out every variable/field alignment. 我试图找出是否有办法用Dapper一次更新一个对象而不必写出每个变量/字段对齐。 Here's an example of what I'm doing now, which is to explicitly spell out each field: 这是我现在正在做的一个例子,即明确拼写每个字段:

    public string UpdateAttributes(List<ItemAttribute> attributesList)
    {     
        try
        {
            using (IfxConnection con = new IfxConnection(WebConfigurationManager.AppSettings["LOKICONN"].ToString()))
            {
                con.Open();
                foreach (ItemAttribute item in attributesList)
                {
                    con.Execute("update oe_cnvwrk set cwr_response = ?, cwr_uom = ? where cwr_genero = ? and cwr_line = ?", 
                        new { cwr_response = item.cwr_response, cwr_uom = item.cwr_uom, cwr_genero = item.cwr_genero, cwr_line = item.cwr_line });
                }
                con.Close();
                return "success";
            }
        }
        catch (Exception x)
        {
            return x.ToString();
        }
    }

Is there a way to skip spelling out each variable and simply reference the object? 有没有办法跳过拼写出每个变量并简单地引用该对象? Or a better way to approach this period? 或者更好的方法来接近这个时期? Dapper allows for dynamically creating an object with a query, and for populating the values of a pre-defined object, but for updating an existing object I'm not finding any documentation or examples. Dapper允许动态创建带有查询的对象,并用于填充预定义对象的值,但是为了更新现有对象,我找不到任何文档或示例。 With a larger object that becomes a bit of a pain, as does maintenance if the table and object need to be changed. 如果需要更改表和对象,则更大的对象会变得有点痛苦。

This might work: 这可能有效:

using (IfxConnection con = new IfxConnection(WebConfigurationManager.AppSettings["LOKICONN"].ToString()))
{
    con.Execute("update oe_cnvwrk set cwr_response = ?cwr_response?, cwr_uom = ?cwr_uom? where cwr_genero = ?cwr_genero? and cwr_line = ?cwr_line?", attributesList);
    return "success";
}

Changes: 变化:

  • no need to open/close the connection 无需打开/关闭连接
  • passes the sequence directly; 直接传递序列; no need to loop 无需循环
  • uses the special dapper ?foo? 使用特殊的小巧玲珑?foo? syntax, which maps named members from the available data to positional SQL; 语法,将命名成员从可用数据映射到位置SQL; this will be re-written to use positional ? 这将重写为使用位置? sql, but adding the expected parameters in the expected order sql,但按预期顺序添加预期参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM