开发者

What is the best approach to bind data to ReportViewer control? (DataAdapter vs DataReader)

I have 2 approaches to bind data to ReportViewer 开发者_如何转开发control. Which one is better? Could you please give me some advice?

Approach 1: Using SqlDataAdapter

Dim dt As DataTable = New DataTable
Dim conn As SqlConnection = New SqlConnection(connString)
Try
    conn.Open()
    Dim cmd As New SqlCommand(sql, conn)
    Dim adapter As New SqlDataAdapter(cmd)
    adapter.Fill(dt)
Catch ex As Exception
Finally
    conn.Close()
End Try

Dim ds As New ReportDataSource(dataSourceName, dt)
rViewer.LocalReport.DataSources.Clear()
rViewer.LocalReport.DataSources.Add(ds)
rViewer.LocalReport.Refresh()

Approach 2: Using SqlDataReader

Dim dt As New DataTable()
Dim conn As SqlConnection = New SqlConnection(connString)
Try
    conn.Open()
    Dim cmd As New SqlCommand(sql, conn)
    Dim reader As SqlDataReader = cmd.ExecuteReader()

    dt.Load(reader)
    reader.Close()
Catch ex As Exception
Finally
    conn.Close()
End Try

Dim ds As New ReportDataSource(dataSourceName, dt)
rViewer.LocalReport.DataSources.Clear()
rViewer.LocalReport.DataSources.Add(ds)
rViewer.LocalReport.Refresh()


If I have to choose between the two I would go for the first one cause a dataset is populated internally using a datareader.
Your second sample converts a datareader in a dataset.
The first option should be slightly faster. Another option would be to use objects (entities) as datasource.
You can find more infos here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜