gpt4 book ai didi

c# - 如何从数据库字段中获取
  • 的值?
  • 转载 作者:太空宇宙 更新时间:2023-11-03 13:20:11 25 4
    gpt4 key购买 nike

    在我的应用程序中,我将 TINYMCE 作为 html 编辑器来接受以下值:

    • 快速类(class)
    • 泽瑞克斯
    • 最佳创新
    • 最佳来源

    插入后,我的数据库中的字段看起来像这样。

    <ul>
    <li>Speedy Course</li>
    <li>Zerix</li>
    <li>Optimum Innovatus</li>
    <li>Optimum Source</li>
    </ul>

    现在我想在访问数据时获取 li 标签的值。

    有没有更快更简单的方法来做到这一点?我能想到的唯一方法是通过一个一个地循环值。顺便说一句,我正在使用 MVC3。

    非常感谢。

    最佳答案

    不确定您是否正在使用 SQL,但如果是,您可以将这两个 CLR 项添加到数据库中......

    [SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true, Name = "RegExSplitIndex",
    SystemDataAccess = SystemDataAccessKind.None, FillRowMethodName = "RegExSplitIndexRow",
    TableDefinition =
    "Match NVARCHAR(MAX), MatchIndex INT, MatchLength INT, NextMatchIndex INT"
    )]
    public static IEnumerable RegExSplitIndex(SqlString input, SqlString pattern, SqlInt32 options)
    {
    if (input.IsNull || pattern.IsNull) return null;
    var matchcol = Regex.Matches(input.Value, pattern.Value, (RegexOptions)options.Value);


    var matches = matchcol.Cast<Match>().ToList().Select(MatchKeys.Translate).ToList();

    if (matches.Any() && matches.All(i => i.MatchIndex != 0))
    {
    var ix = matches.OrderBy(i => i.MatchIndex).First().MatchIndex;
    matches.Add(new MatchKeys() {MatchIndex = 0, MatchLength = 0, NextMatchIndex = ix});

    }

    return matches;
    }

    public static void RegExSplitIndexRow(Object input, ref SqlString match, ref SqlInt32 matchIndex, ref SqlInt32 matchLength, ref SqlInt32 nextMatchIndex)
    {
    MatchKeys m = (MatchKeys)input;
    match = new SqlString(m.Match);
    matchIndex = m.MatchIndex;
    matchLength = m.MatchLength;
    nextMatchIndex = m.NextMatchIndex;
    }

    然后在您的 sql 查询中,您可以使用像这样的拆分函数来拆分 html 标签:

    OUTER APPLY [dbo].RegExSplitIndex(<YourdbField>),'(<(?:"[^"]*"[''"]*|''[^'']*''[''"]*|[^''">])+>)|\n',0) REM

    并检查匹配值 ([REM].[Match] = '<li>')

    您可以使用匹配索引和长度来了解您需要从哪里开始从拆分字符串中获取实际值

    如果您无法访问 CLR,那么您仍然可以使用正则表达式进行拆分,并使用类似的策略在您的代码中完成所有操作

    关于c# - 如何从数据库字段中获取 <li> 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604526/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com