VB.NET SQL ExecuteReader。 [英] VB.NET SQL ExecuteReader.

查看:540
本文介绍了VB.NET SQL ExecuteReader。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有没有一种简单的方法可以使用字段名称在SQL中返回值。

我有一个函数执行SELECT脚本。

Hello everyone,

Is there an easy way to get the values return in SQL using field names.
I have a function that executes a SELECT script.

Public Function ExecuteQuery(ByVal SQL As String) As DataSet
    Dim dt As New DataSet
    Try
        If SQLConn.State = ConnectionState.Closed Then SQLConn.Open()
        Dim SQLCmd As New SqlCommand
        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQL
        SQLCmd.ExecuteReader()
        SQLCmd.Connection.Close()
        Dim SQLAdp As New SqlDataAdapter(SQLCmd)
        SQLAdp.Fill(dt)
    Catch ex As Exception
        dt = Nothing
    End Try
    Return dt
End Function



这是我获取字段值的方式


This is how I get the field values

Dim dt As DataSet = ExecuteQuery(My.Resources.AddressFile.Replace("%%", trvSearch.SelectedNode.Tag))
MessageBox.Show(dt.Tables(0).Rows(0).Item(0))



我可以使用字段名称获取数据:


Can I get data using field name:

dt.items("Field01")



SqlClient中有ResultSet吗?



请帮助...谢谢


Is there a ResultSet in SqlClient?

Please help... Thanks

推荐答案




是的你可以用 DataReader 对象:



Hi
Yes you can do it with DataReader object:

Public Function ExecuteQuery(ByVal SQL As String) As DataSet
      Dim dr As New SqlDataReader ' or OleDataReader
      Dim List<object> myReaderList = new List<object>()
      Try
          If SQLConn.State = ConnectionState.Open Then SQLConn.Close()
          Dim SQLCmd As New SqlCommand
          ConnectionState.Open
          SQLCmd.Connection = SQLConn
          SQLCmd.CommandText = SQL
          dr = SQLCmd.ExecuteReader()
          while(dr.Read())
             myReaderList.Add(dr.Read("FiealName"))
          end while
          SQLCmd.Connection.Close()
          ' you can remove SQLAdp and dt maybe you don`t need that ;)
          Dim SQLAdp As New SqlDataAdapter(SQLCmd)
          SQLAdp.Fill(dt)
      Catch ex As Exception
          dt = Nothing
      End Try
      Return dt
  End Function</object></object>





当然,如果你关闭连接,你应该注意使用读者读者所以关闭连接 $ c>。

最好的问候。



Of course you should note if you are close the connection you will lost reader so close connection after finished your work with reader.
Best Regards.


这篇关于VB.NET SQL ExecuteReader。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆