利用KeyValuePair生成sql键值对csharpVS2015

环境 VS2015 C# 命令行语句测试


如题: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    /// <summary>
    /// 随便定一个
    /// </summary>
    public class MyMethod
    {
        public MyMethod()
        {
        }
        

        //采用排序的Dictionary的好处是方便对数据包进行签名,不用再签名之前再做一次排序
        private SortedDictionary<string, object> m_values = new SortedDictionary<string, object>();

        /**
        * 设置某个字段的值
        * @param key 字段名
         * @param value 字段值
        */
        public void SetValue(string key, object value)
        {
            m_values[key] = value;
        }

        /**
        * 根据字段名获取某个字段的值
        * @param key 字段名
         * @return key对应的字段值
        */
        public object GetValue(string key)
        {
            object o = null;
            m_values.TryGetValue(key, out o);
            return o;
        }

        /**
         * 判断某个字段是否已设置
         * @param key 字段名
         * @return 若字段key已被设置,则返回true,否则返回false
         */
        public bool IsSet(string key)
        {
            object o = null;
            m_values.TryGetValue(key, out o);
            if (null != o)
                return true;
            else
                return false;
        }
        /**
        * @获取Dictionary
        */
        public SortedDictionary<string, object> GetValues()
        {
            return m_values;
        }

        /*-------------------------------------------------------*/
        /**
        * @values生成插入语句
        */
        public string ToSQLStr_Insrt(string dataTable, string condition)
        {
            string str1 = "";
            string str2 = "";
            foreach (KeyValuePair<string, object> pair in m_values)
            {
                if (pair.Value == null)
                {
                    throw new Exception("Data内部含有值为null的字段!");
                }
                string[] type = pair.Key.ToString().Split(',');
                str1 += string.Format(",`{0}`", type[1]);
                string Value = pair.Value.ToString();
                switch (type[0])
                {
                    case "string":
                        {
                            str2 += string.Format(",'{0}'", Value);
                        }
                        break;
                    case "double":
                        {
                            str2 += string.Format(",{0:N2}", double.Parse(Value));
                        }
                        break;
                    case "decimal":
                        {
                            str2 += string.Format(",{0:N2}", decimal.Parse(Value));
                        }
                        break;
                    case "int":
                        {
                            str2 += string.Format(",{0:N}", int.Parse(Value));
                        }
                        break;
                    default:
                        break;
                }
            }
            string sqlstr = "INSERT INTO `" + dataTable + "` (" + str1.Substring(1) + ") VALUES(" + str2.Substring(1) + ") " + condition;
            return sqlstr;
        }
        /**
       * @values生成删除语句
       */
        public string ToSQLStr_Delete(string dataTable, string condition)
        {
            string sqlstr = "DELETE FROM `" + dataTable + "` " + condition;
            return sqlstr;
        }
        /**
        * @values格式化成能在Web页面上显示的结果(因为web页面上不能直接输出xml格式的字符串)
        */
        public string ToSQLStr_Select(string dataTable, string condition)
        {
            string str = "";
            foreach (KeyValuePair<string, object> pair in m_values)
            {
                str += string.Format(",`{0}`", pair.Key.ToString());
            }
            string sqlstr = "SELECT " + str.Substring(1) + "  FROM `" + dataTable + "` " + condition;
            return sqlstr;
        }
        /**
        * @values生成修改语句
        */
        public string ToSQLStr_Update(string dataTable, string condition)
        {
            string str = "";
            foreach (KeyValuePair<string, object> pair in m_values)
            {
                if (pair.Value == null)
                {
                    throw new Exception("Data内部含有值为null的字段!");
                }
                string[] type = pair.Key.ToString().Split(',');
                string Value = pair.Value.ToString();
                switch (type[0])
                {
                    case "string":
                        {
                            str += string.Format(",`{0}`='{1}'", type[1], Value);
                        }
                        break;
                    case "double":
                        {
                            str += string.Format(",`{0}`='{1:N4}'", type[1], double.Parse(Value));
                        }
                        break;
                    case "decimal":
                        {
                            str += string.Format(",`{0}`='{1:N2}'", type[1], decimal.Parse(Value));
                        }
                        break;
                    case "int":
                        {
                            str += string.Format(",`{0}`={1:N}", type[1], int.Parse(Value));
                        }
                        break;
                    default:
                        break;
                }
            }
            string sqlstr = "UPDATE `" + dataTable + "`  SET " + str.Substring(1) + " " + condition;
            return sqlstr;
        }
    }
}


调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyMethod data = new MyMethod();
            data.SetValue("string,appid", "111111");//类型,建,值
            data.SetValue("double,mch_id", 2222.23156);//类型,建,值
            data.SetValue("int,time_stamp", 333);//类型,建,值
            data.SetValue("double,nonce_str", 444.00);//类型,建,值
            data.SetValue("string,product_id", "55555555");//类型,建,值
            Console.WriteLine(data.ToSQLStr_Insrt("cashier_goods", ""));
            Console.WriteLine(data.ToSQLStr_Update("cashier_goods", "where a=b"));
            Console.WriteLine(data.ToSQLStr_Delete("cashier_goods", "where a=b"));
            data = new MyMethod();
            data.SetValue("appid", null);//类型,建,值
            data.SetValue("mch_id", null);//类型,建,值
            data.SetValue("time_stamp", null);//类型,建,值
            data.SetValue("nonce_str", null);//类型,建,值
            data.SetValue("product_id", null);//类型,建,值
            Console.WriteLine(data.ToSQLStr_Select("cashier_goods", "where a=b"));
            Console.ReadLine();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值