• C#


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    public class Program
    {
    	public static void Main()
    	{
    		string strID = "10010";
    		Dictionary<string, string> g = new Dictionary<string, string>();
    		g.Add("K", "k1");
    		g.Add("L", "l1");
    		string updateCustomerIncome = string.Format("  update CustomerIncome set {0} where ID = '{1}'; ",
                        string.Join(", ", g.Select(x => String.Format("{0} = '{1}'", x.Key, x.Value))), strID);
    		Console.WriteLine(updateCustomerIncome);
    		
    		//var result2 = string.Join(", ", g.Select(x => String.Format("{0} = '{1}'", x.Key, x.Value)));
    		//Console.WriteLine(result2);
    		
    		//Console.WriteLine(string.Join(", ", new List<string>(g.Keys)));
    		//Console.WriteLine(string.Join(", ", new List<string>(g.Values)));
    		string insertCustomerIncome = string.Format("  insert CustomerIncome(ID, {0}) values ('{1}', '{2}');", 
    					string.Join(", ", new List<string>(g.Keys)), strID, string.Join("', '", new List<string>(g.Values)));
    		Console.WriteLine(insertCustomerIncome);
    		
    		StringBuilder sb = new StringBuilder();	
    		sb.AppendFormat("if exists (select * from CustomerIncome where id = '{0}')", strID);
    		sb.AppendLine();
    		sb.AppendLine(updateCustomerIncome);
    		sb.AppendLine("else");
    		sb.Append(insertCustomerIncome);
    		
    		Console.WriteLine(sb.ToString());
    	}
    }
    

    the output

      update CustomerIncome set K = 'k1', L = 'l1' where ID = '10010'; 
      insert CustomerIncome(ID, K, L) values ('10010', 'k1', 'l1');
    if exists (select * from CustomerIncome where id = '10010')
      update CustomerIncome set K = 'k1', L = 'l1' where ID = '10010'; 
    else
      insert CustomerIncome(ID, K, L) values ('10010', 'k1', 'l1');
    

    references:
    https://dotnetfiddle.net/uiFWXo
    How to get the list of key in dictionary

  • 相关阅读:
    字符串的全排列

    链表
    青蛙跳一格或者两格,n格跳法
    二叉树
    Concurrent实现原理
    sql语句总结 (转) http://blog.csdn.net/fengfeng91/article/details/15029173
    ArrayList实现原理
    java虚拟机 内存分配
    【转】关于Quartus ii无法识别Modelsim路径的问题
  • 原文地址:https://www.cnblogs.com/grj1046/p/5250064.html
Copyright © 2020-2023  润新知