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

本文详细解析了如何从SQL中提取并处理复杂的枚举信息,包括使用正则表达式匹配特定模式,以及如何根据提取的数据生成SQL条件语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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; }
    }
}

 

转载于:https://www.cnblogs.com/huangzelin/p/4495368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值