Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports Microsoft.Win32
Imports Microsoft.Reporting.WinForms
Imports System.Data.OleDb
Public Class Form2
Dim con As New OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet("myDataSet")
Dim dt As New DataTable
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connect()
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Call create_report()
End Sub
Sub connect()
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source = D:\test.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
End Sub
Function sqlSelect(ByRef sqlString As String)
da = New OleDbDataAdapter(sqlString, con)
da.Fill(ds, "Results")
Return ds
End Function
Public Sub create_report()
Dim str As String
Dim substr As String
str = xOD.Value.ToShortDateString
substr = (Microsoft.VisualBasic.Left(str, 7))
Dim Sql As String
Sql = "SELECT * FROM test_data"
ds = sqlSelect(Sql)
End Sub
End Class
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString, strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "SELECT * FROM customer WHERE CountryCode LIKE '%" & Me.txtCountryCode.Text & "%' "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("myDataSet_myDataTable", dt))
ReportViewer1.LocalReport.ReportPath = "myReport.rdlc"
ReportViewer1.LocalReport.Refresh()
ReportViewer and DataSet ออกรายงานกับการสร้าง DataSource ด้วย DataSet