根據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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章