如何从数据库字段中获取的值?

时间:2022-03-12 22:28:09

In my application, I have TINYMCE as an html editor to accept this values:

在我的申请中,TINYMCE作为html编辑器接受以下值:

  • SpeedyCourse
  • SpeedyCourse
  • Zerix
  • Zerix
  • Optimum Innovatus
  • 最佳Innovatus
  • Optimum Source
  • 最佳来源

Once inserted, the field in my database looks something like this.

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

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

Now I want to get the value of the the li tags when I access the data.

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

Is there a fast and easier way to do this? The only way I can think of is through looping the values one by one. I'm using MVC3 by the way.

有没有一种快速简单的方法来做这件事?我能想到的唯一方法是通过一个一个地循环这些值。顺便说一下,我用的是MVC3。

Many thanks in advance.

提前感谢。

1 个解决方案

#1


1  

Not sure if you are using SQL or not but if so you can add these two CLR Items to the Database...

不确定您是否正在使用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;
} 

Then In your sql query you can use can use the Split Function like this to split out html tags:

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

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

And check to see what the the match value ([REM].[Match] = '<li>')

检查匹配值([REM])。(比赛)= <李> )

you can use the match index and length to know where you need to start to get the actual value from the split string

您可以使用匹配索引和长度来了解从分割字符串中开始获取实际值的位置

if you don't have access to CLR then you can still use regex to split and do it all from within your code using a similar strategy

如果您没有访问CLR的权限,那么您仍然可以使用regex进行拆分,并使用类似的策略在代码中执行所有操作

#1


1  

Not sure if you are using SQL or not but if so you can add these two CLR Items to the Database...

不确定您是否正在使用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;
} 

Then In your sql query you can use can use the Split Function like this to split out html tags:

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

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

And check to see what the the match value ([REM].[Match] = '<li>')

检查匹配值([REM])。(比赛)= <李> )

you can use the match index and length to know where you need to start to get the actual value from the split string

您可以使用匹配索引和长度来了解从分割字符串中开始获取实际值的位置

if you don't have access to CLR then you can still use regex to split and do it all from within your code using a similar strategy

如果您没有访问CLR的权限,那么您仍然可以使用regex进行拆分,并使用类似的策略在代码中执行所有操作