根据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;
}
{
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;
}