使用查询将数据库连接到vb代码中的数据表

时间:2013-03-06 10:01:47

标签: sql database vb.net

我想使用select query连接数据库中的表。连接字符串已在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

我很慷慨,提前谢谢

1 个答案:

答案 0 :(得分: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 *在生产代码中通常是一个坏主意。明确列出您想要的列