根據json內容更新表的一行,欄位數量不固定,但名稱需要一致

chengeng發表於2024-04-26
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;
}

相關文章