使用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代码帮助
答案 0 :(得分: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
并且瞧瞧seez