0

我想使用选择查询连接数据库中的表。页面上已经建立了连接字符串web.config

<connectionStrings>
    <add name="IntranetConnectionString" connectionString="Data Source=nanana\sql5;Initial Catalog=Intranet;User ID=intra_admin;password=jimfool" providerName="System.Data.SqlClient"/>
</connectionStrings>

我想创建一个名为 GetList 的函数,只要我想绑定信息,我就可以在其中使用它。这是我的VB代码

Public Function GetList() As DataTable
    Dim Query As String
    Dim dt As DataTable
    Query = "Select * FROM Intranet_Gn_ISCoordinators"

    'I need to write something here ..
    'dt = 
    Return dt
End Function

我是大人物,在此先感谢

4

1 回答 1

2

您可以使用SqlDataAdapter

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Function GetList() As DataTable

    Dim Query As String = "Select * FROM Intranet_Gn_ISCoordinators"
    Dim dt As DataTable = New DataTable()

    Using adapter = New SqlDataAdapter(Query, ConfigurationManager.ConnectionStrings["IntranetConnectionString"].ConnectionString)
        adapter.Fill(dt)
        return dt
    End Using

End Function

顺便说一句,我意识到查询可能只是一个示例,但SELECT *在生产代码中通常是一个坏主意。明确列出您想要的列

于 2013-03-06T10:45:09.463 回答