简体   繁体   English

我如何在vb.net中更改字符串名称列表

[英]How can i change a list of string name In vb.net

i have a WebMethod that you pass a SQL Query for execution and return a list of string 我有一个WebMethod ,您通过SQL查询执行并返回字符串列表

  • The query comes from the client side so i don't know what it is 该查询来自客户端,所以我不知道它是什么

Here is the webMethod 这是webMethod

<WebMethod()> _
Public Function ExecQuery() As List(Of String)

    Dim result As New List(Of String)
    Dim sqlConn As New sqlConn........
    Dim queryString As String = 'SELECT * FROM tbl_name'
    Dim command As New SqlCommand(queryString, sqlConn)
    sqlConn.Open()
    Dim reader As SqlDataReader = command.ExecuteReader()
    While reader.Read()
        For i As Integer = 0 To reader.FieldCount - 1
            result.Add(reader.Item(reader.GetName(i)))
        Next

    End While
    Return result

End Function

This code return a list of string as follow 此代码返回字符串列表,如下所示

在此处输入图片说明

i don't have enough experience doing this 我没有足够的经验

QUESTION

What is the best way of organizing this 最好的组织方式是什么

  • How can i change <string> To <column_name> and wrap them with Unique Name 如何将<string>更改为<column_name>并用Unique Name包装

    i used to create a Class and instead of list (of string) i used the class reference 我用来创建一个Class ,而不是list (of string)我使用了类引用

and i get a list as follow 我得到如下列表

 <className>
     <col1>val1<col1>
     <col2>val1<col1>
 </className>
 <className>
     <col1>val2<col1>
     <col2>val2<col1>
 </className>

and then i loop in javascript using this class name 然后我使用此类名称在JavaScript中循环

If this needs to be completely dynamic, then (as @Jeremy suggested) return your own XML. 如果需要完全动态,则(如@Jeremy所建议的)返回您自己的XML。 Have the webmethod return String , not List(Of ...) . 让webmethod返回String ,而不是List(Of ...) Create your own XML string: 创建自己的XML字符串:

Dim sb As New System.Text.StringBuilder
While reader.Read()
    sb.Append("<record>")
    For i As Integer = 0 To reader.FieldCount - 1
        sb.AppendFormat("<field name=""{0}"">", reader.GetName(i))
        sb.Append(reader(i).ToString())
        sb.Append("</field>")
    Next
    sb.Append("</record>")
End While

Return sb.ToString()

Parse the XML in your Javascript code using DOM methods. 使用DOM方法解析Javascript代码中的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM