在表中拆分nvarchar

时间:2018-06-04 11:10:13

标签: .net vb.net

我有一个返回300行数据的数据表。我想要做的是找到一个特定的列并将该列的内容拆分为&#34; <br />&#34;然后添加<a href=...>&amp;每个拆分周围</a>,然后返回相同的数据表。

这是我到目前为止所做的:

 Private Sub SplitCellsWithAnchor(ByVal dt As DataTable)

        Dim BroaderTerms As String() = dr.Item("BroaderMain").ToString.Trim.Split(CType("<br />", Char()))
        Dim Replace As String
        For Each b As String In BroaderTerms
            If b.Trim <> "" Then
                Replace = "<a href='' target='_blank'>" & b.Trim & "</a>"
            End If
        Next
    End Sub

根据我的理解,我必须给它一个数据表,但是如何得到那个特定的专栏我不太确定 - 我应该怎么做?

1 个答案:

答案 0 :(得分:0)

我假设您已经从数据库中获取了数据并将其存储在名为MyDataTable的表中,并且数据表中的列名称称为MyColumn

 ' Untested Code
 Dim MyListOfStrings as List(of String())
 For Each MyDataRow as DataRow in MyDataTable.Rows
    'Now you jsut access the column name and do what you need with it.
    Dim MyDtring as String = MyDataRow("MyColumn")
    If Not IsDbnull(MyString) Then ' Always check for null
        ' Because we don't know how many "<br /"'s there are, we loop through them all (untested code)
        Dim MyStringArray() as string = Split(MyString, "<br />")
        Dim x as Integer = 0
        While X <= MyStringArray.Length - 1
            MyStringArray(x) = "<a href='' target='_blank'>" & MyStringArray(x) & "</a>"
            x += 1
        Loop
        MyListOfStrings.Add(MyStringArray)
        ' When complete you will have 300 string arrays in the list of strings
    End If
Next