首页 > 代码库 > SQL动态配置,动态解析SQL

SQL动态配置,动态解析SQL

在项目中使用SQL动态配置的方式可以让后期的维护和发布后的修改变得更加方便,无论使用那种配置方式都离不开解析成最终真正能执行的SQL。
下面代码就是一种比较简单的处理方法,SQL的参数以##括起来。

1.在代码中先建立一个需要解析的SQL,后面需要变成配置方式

/// <summary>/// 暂时用于测试/// </summary>static string _sql = "SELECT * FROM TB_USER WHERE ID = #ID# AND NAME LIKE ‘%#NAME#%‘";

2.建立一个根据参数KEY去解析的方法,一般情况下都是使用此方法

/// <summary>/// 根据SQL和参数集合解析成需要执行的SQL/// </summary>/// <param name="str"></param>/// <param name="dic"></param>/// <returns></returns>public static string AnalyticalByDic(string str, Dictionary<string, string> dic){    str = str.ToUpper();    foreach (var item in dic)    {        str = str.Replace(string.Format("#{0}#", item.Key.ToUpper()), item.Value);    }    return str;}

3.建立一个根据顺序解析的方法,不推荐使用此方法

/// <summary>/// 根据SQL和参数集合解析成需要执行的SQL/// </summary>/// <param name="str"></param>/// <param name="list"></param>/// <returns></returns>public static string AnalyticalByList(string str, List<string> list){    str = str.ToUpper();    List<string> sqlList = new List<string>();    var sqlstr = str;    int i = 0;    int j = 0;    int sum = 0;    while (true)    {        i = str.Substring(sum, str.Length - sum).IndexOf(#) + 1;        sum += i;        j = str.Substring(sum, str.Length - sum).IndexOf(#);        if (i < 0 || j < 0)        {            break;        }        sqlList.Add(str.Substring(sum, j));        sum += j + 1;    }    for (int index = 0; index < sqlList.Count && index < list.Count; index++)    {        str = str.Replace(string.Format("#{0}#", sqlList[index].ToUpper()), list[index]);    }    return str;}

4.方法建好了,就试一下DEMO看看效果,直接在Main里面调用就好

static void Main(string[] args){    Dictionary<string, string> dic = new Dictionary<string, string>();    dic.Add("ID", "123");    dic.Add("Name", "jimmy.pan");    string sql = _sql;    Console.WriteLine(AnalyticalByDic(sql, dic));    List<string> list = new List<string>();    list.Add("456");    list.Add("jimmy");    sql = _sql;    Console.WriteLine(AnalyticalByList(sql, list));    Console.Read();}

5.运行结果如下图

技术分享

6.整体代码

using System;using System.Collections.Generic;namespace SQLAnalytical{    class Program    {        static void Main(string[] args)        {            Dictionary<string, string> dic = new Dictionary<string, string>();            dic.Add("ID", "123");            dic.Add("Name", "jimmy.pan");            string sql = _sql;            Console.WriteLine(AnalyticalByDic(sql, dic));            List<string> list = new List<string>();            list.Add("456");            list.Add("jimmy");            sql = _sql;            Console.WriteLine(AnalyticalByList(sql, list));            Console.Read();        }        /// <summary>        /// 根据SQL和参数集合解析成需要执行的SQL        /// </summary>        /// <param name="str"></param>        /// <param name="dic"></param>        /// <returns></returns>        public static string AnalyticalByDic(string str, Dictionary<string, string> dic)        {            str = str.ToUpper();            foreach (var item in dic)            {                str = str.Replace(string.Format("#{0}#", item.Key.ToUpper()), item.Value);            }            return str;        }        /// <summary>        /// 根据SQL和参数集合解析成需要执行的SQL        /// </summary>        /// <param name="str"></param>        /// <param name="list"></param>        /// <returns></returns>        public static string AnalyticalByList(string str, List<string> list)        {            str = str.ToUpper();            List<string> sqlList = new List<string>();            var sqlstr = str;            int i = 0;            int j = 0;            int sum = 0;            while (true)            {                i = str.Substring(sum, str.Length - sum).IndexOf(#) + 1;                sum += i;                j = str.Substring(sum, str.Length - sum).IndexOf(#);                if (i < 0 || j < 0)                {                    break;                }                sqlList.Add(str.Substring(sum, j));                sum += j + 1;            }            for (int index = 0; index < sqlList.Count && index < list.Count; index++)            {                str = str.Replace(string.Format("#{0}#", sqlList[index].ToUpper()), list[index]);            }            return str;        }        /// <summary>        /// 暂时用于测试        /// </summary>        static string _sql = "SELECT * FROM TB_USER WHERE ID = #ID# AND NAME LIKE ‘%#NAME#%‘";    }}

 

SQL动态配置,动态解析SQL