根据json内容更新表的一行,字段数量不固定,但名称需要一致

public static int Update(string strJson)
{
    int count = 0;
    List<Dictionary<string, object>> listRows = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(strJson);
    foreach (var row in listRows)
    {
        var dic = row.ToDictionary(item => item, item => true);
        string strUpdateKey = "";
        string strUpdateKeyValue = "";

        StringBuilder sb_Set = new StringBuilder("");
        foreach (var col in dic)
        {
            string strKey = col.Key.Key;
            string strValue = col.Key.Value.ToString();

            // 用于where 字段和值的保存
            if (strKey == "KeyID")
            {
                strUpdateKey = strKey;
                strUpdateKeyValue = strValue;
            }
            if (strKey == "terminalWorkOrderProductionWeftinfoVoList")
            {
                continue;
            }

            if (sb_Set.ToString() == "")
            {
                sb_Set.Append(string.Format(strKey + "='{0}'", strValue));
            }
            else
            {
                sb_Set.Append(string.Format("," + strKey + "='{0}'", strValue));
            }
        }

        string strWhere = string.Format(" where {0} = '{1}';", strUpdateKey, strUpdateKeyValue);
        StringBuilder sb_Sql = new StringBuilder("update ProductPlan set ");
        sb_Sql.Append(sb_Set.ToString());
        sb_Sql.Append(strWhere);
        count = count + SQLiteHelper.ExecuteNonQuery(sb_Sql.ToString());
    }
    return count;
}
posted @ 2024-04-26 08:09  chengeng  阅读(27)  评论(0编辑  收藏  举报