我是VB.net的新手,我需要创建通用函数来检索数据行。我试过这种方式。但这并不成功。
Function GetRecord(ByVal sql As String) As DataRow
Dim conn As MySqlConnection = connect()
Dim objDataSet As New DataSet
Dim dt As DataTable
Dim dr As DataRow
Try
Dim cmd As New MySqlCommand()
conn.Open()
cmd.Connection = conn
cmd.CommandText = sql
Dim da As New MySqlDataAdapter
da.SelectCommand = cmd
Dim ds As New DataSet
da.Fill(ds, "T")
dt = ds.Tables("T")
dr = dt.Rows.Item(0)
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dr
End Function
备注:Connect()函数返回mysql连接对象
答案 0 :(得分:0)
不完全确定您要做什么,但将代码更改为
da.Fill(dt, "T")
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dt.Rows.Item(0)