~工作簡化,不再是難事~

2016-06-04

〔ASP.NET〕Convert sqlDataReader to DataTable / 將DataReader資料轉入DataTable

Dim dt As New DataTable
dt.Load(dr)        




範例:
        Dim Conn As SqlConnection = DBLink .setConn("ConnectionString" )
        Dim strSQL As String = ""
        Dim cmd As SqlCommand = Nothing
        Dim dr As SqlDataReader = Nothing
        Try
           
             strSQL = "SELECT * FROM Table"

            cmd = New SqlCommand(strSQL, Conn)
            dr = cmd.ExecuteReader
            If dr.HasRows Then
                Dim dt As New DataTable
                dt.Load(dr)
                Return dt
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Return Nothing
        Finally
            PublicFunction.iniDispose(cmd, dr, Conn)

        End Try

0 意見:

張貼留言

Popular Posts

Copyright © 2016 Scenic's BOX. 技術提供:Blogger.