0

使用 VB.Net

我想通过函数获取表值

检索表值的函数

Function GetFsDtValue(ByVal value As Integer) As String
        Dim conObjects As New ConnectionObjects
        conObjects.OpenConnection()

            Dim objDataAdapter As New SqlDataAdapter("Select * from table1", conObjects.myConnection)
            Dim pobjDS As New DataSet("Result")
            If Not IsNothing(objDataAdapter) Then
                objDataAdapter.Fill(pobjDS)
                objDataAdapter.Dispose()
            End If
            If pobjDS.Tables.Count > 0 AndAlso pobjDS.Tables(0).Rows.Count > 0 Then
                GetFsDtValue = pobjDS.Tables(0).Rows(0).Item(value)
            Else
                GetFsDtValue = ""
            End If

        End Try
    End Function

我多次调用上述函数

txtbox1.Text = GetFsDtValue(7)
txtbox2.Text = GetFsDtValue(8)

每次我调用该函数时,它都会再次打开连接并将表保存在数据集中,这会影响性能。任何其他替代方式

我想通过函数调用表值。怎么做

需要vb.net代码帮助

4

1 回答 1

1

创建一个执行此操作的函数并将其称为 FetchData 或其他名称:

Dim conObjects As New ConnectionObjects
        conObjects.OpenConnection()

            Dim objDataAdapter As New SqlDataAdapter("Select * from table1", conObjects.myConnection)
            _pobjDS As New DataSet("Result")
            If Not IsNothing(objDataAdapter) Then
                objDataAdapter.Fill(pobjDS)
                objDataAdapter.Dispose()
            End If´

让 pobjDS 成为这样的全局变量:

private _pobjDS as DataSet

在 GetFsDtValue 中这样做:

Function GetFsDtValue(ByVal value As Integer) As String

            If _pobjDS.Tables.Count > 0 AndAlso _pobjDS.Tables(0).Rows.Count > 0 Then
                GetFsDtValue = _pobjDS.Tables(0).Rows(0).Item(value)
            Else
                GetFsDtValue = ""
            End If

        End Try
    End Function

瞧瞧瞧瞧

于 2012-11-02T15:42:04.437 回答