Pages

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