将odbcReader分配给dataSet

时间:2014-10-09 11:01:50

标签: mysql vb.net datatable dataset sqldatareader

我已经执行了存储过程

Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
Dim con As New OdbcConnection(conStr)
Dim cmd As New OdbcCommand("dat.selectRowCount", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@startIndex", 5)
cmd.Parameters.AddWithValue("@endIndex", 15)
Dim reader As OdbcDataReader
reader = cmd.ExecuteReader
While reader.Read
'Some operations performed
End While

如果我需要将此reader添加到DataTable表示

Dim dt As DataTable = New DataTable("adm_ttemp")
dt.Load(reader)

但是如何将此阅读器分配给DataSet

2 个答案:

答案 0 :(得分:0)

我说要稍微改变一下你的方法,并使用OdbcDataAdapter如下:

Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
Dim con As New OdbcConnection(conStr)
Dim cmd As New OdbcCommand("dat.selectRowCount", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@startIndex", 5)
cmd.Parameters.AddWithValue("@endIndex", 15)
Dim DS as new DataSet
Using AD as new OdbcDataAdapter(cmd)
  AD.Fill(DS)
End Using
' Perform your operations on the DataSet

答案 1 :(得分:0)

为什么要寻找odbcReader to a dataSet作业?您可以直接将查询结果读取到dataset,如上面的答案所述?

    Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
    Dim con As New OdbcConnection(conStr)
    Dim cmd As New OdbcCommand("dat.selectRowCount", con)
    con.Open()
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@startIndex", 5)
    cmd.Parameters.AddWithValue("@endIndex", 15)
    Dim DS As New DataSet
    '<---- lines to be noticed 
    Using AD As New OdbcDataAdapter(cmd)
        AD.Fill(DS)
    End Using

您可以通过以下方式调用存储过程,只需尝试

即可
    Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
    Dim con As New OdbcConnection(conStr)
    Dim cmd As New OdbcCommand("dat.selectRowCount", con)
    con.Open()
    '<----
    Dim callString As String = "CALL dat.selectRowCount(5,15)"
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = callString
    '<-----
    Dim dlist As New DataList
    Dim DS As New DataSet
    Using AD As New OdbcDataAdapter(cmd)
        AD.Fill(DS)
    End Using