将枚举定义生成SQL中的Case-When-then语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace TestPro
{
    public partial class CaseWhenSqlGeneration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string enumString = this.txtEnum.Text.Trim().Replace("\r\n","");
            bool useMark = this.chkUseMark.Enabled;
            string result = string.Empty;
            List<EnumInfo> enumInfos = new List<EnumInfo>();

            string regString = "(?:(?:\\s*///\\s*<summary>)\\s*///\\s*(?<mark>[\\S]*?)(?:\\s*///\\s*</summary>))*\\s*((?<key>[\\S]+)\\s*=\\s*(?<value>[\\d]+))";
            Regex regex = new Regex(regString, RegexOptions.None);

            MatchCollection matchs = regex.Matches(enumString);
            foreach (Match match in matchs)
            {
                enumInfos.Add(new EnumInfo
                {
                    Mark = match.Groups["mark"].Value,
                    Key = match.Groups["key"].Value,
                    Value = match.Groups["value"].Value
                });
            }

            foreach (var item in enumInfos)
            {
                if (this.chkUseMark.Checked)
                {
                    result += string.Format("\r\n when {0} then ‘{1}‘ ", item.Value, string.IsNullOrEmpty(item.Mark) ? item.Key : item.Mark);
                }
                else
                {
                    result += string.Format("\r\n when {0} then ‘{1}‘ ", item.Value, item.Key);
                }
            }

            if (enumInfos != null) { result += "\r\n else ‘未知枚举‘ end"; }
            this.txtResult.Text = result;
        }
    }

    public class EnumInfo
    {
        public string Mark { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。