【问题标题】:Assign a odbcReader to a dataSet将 odbcReader 分配给数据集
【发布时间】:2014-12-04 06:50:33
【问题描述】:

我执行了一个存储过程

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?

【问题讨论】:

    标签: mysql vb.net datatable dataset sqldatareader


    【解决方案1】:

    您为什么要寻找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
    

    【讨论】:

      【解决方案2】:

      我想说稍微改变一下你的方法,并使用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
      

      【讨论】:

      • dataset 在哪里?只有dataTable 和DataAdapter 在这里吗?
      • @NeethuSoman 我的错误,更新了答案。 (我通常在工作中使用 DataTables 而不是 DataSets,因此会造成混淆)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 2016-06-07
      • 2014-07-17
      • 2018-09-26
      相关资源
      最近更新 更多