You all ever make a form to display or record a very large load on the grid for example in a grid have to load about 4 -10 million records? Sure enough khan also troublesome, especially for application performance. First saia already done it with TrueDBGrid (time still use VB6) which should load the millions of records into a single grid, it is quite risky for the performance … fortunately still helped with data retrieval model is asyncfect ado … But still a big risk for application performance, not to mention if the application is multiuser … can be more severe.
It turns out the solution of that is using paging in the DataAdapter or the Stored Procedure (TableAdapter).
With this technique application performance is getting good, safe and stable.
Here paging technique using Table Adapter (Stored Procedure) in VB.Net:
Ok … first we make the stored procedurenya first. This procedure Storde who do paging. It also used a new feature in SQL Server 2005 is the Common Table Expression (CTE) that we can use to create a virtual table (create table on the fly).
Used the AdventureWorks database, table table Production.Product
- Make stored procedure
1234567891011Create Proc GetProductPaging@from int, @to intAsWith cteProductAs(Select row_number() Over(Order By ProductID ASC) As recnum,[Name],ProductNumberFrom Production.<a href="http://astroalloys.com.au/my-account/">Product</a>)Select * From cteProduct Where recnum Between @from And @to - After that create a design form as below (label, numericupdown, Button, DataGridView)
- Add TableAdapter with a click on the Data menu> Add New Data Source, select Database as its DataSource object … After that, select Stored Procedure that have been made over … then the result will be creating a dataset with a TableAdapter
- Now we began to make his code. Add this code within the Form class is used:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172Imports System.Data.SqlClientPublic Class Form1Private intStartRec, intRecPerPage, intTotalRec As IntegerPrivate taProduct As AdventureWorksDataSetTableAdapters.GetProductPagingTableAdapterPrivate tblProduct As DataTablePrivate Sub GetData(ByVal startRecord As Integer, ByVal maxRecord As Integer)taProduct = New AdventureWorksDataSetTableAdapters.GetProductPagingTableAdaptertblProduct = taProduct.GetData(startRecord, maxRecord)DataGridView1.DataSource = tblProductEnd SubPrivate Sub SetRecordLabel()Label2.Text = "Start Record Number : " + (intStartRec + 1).ToString + _" From total record : " + intTotalRec.ToStringEnd SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadintRecPerPage = NumericUpDown1.Value '//number of records per pageintStartRec = 0 '//initial record <a href="http://www.info-vijesti.com/">index</a>GetData(intStartRec, intRecPerPage)intTotalRec = taProduct.GetTotalRecordSetRecordLabel()End SubPrivate Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.ClickintRecPerPage = NumericUpDown1.ValueintStartRec = 0GetData(intStartRec, intRecPerPage)SetRecordLabel()End SubPrivate Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.ClickintRecPerPage = NumericUpDown1.ValueintStartRec -= intRecPerPage'check if the start index record = 0 ThenGetData(intStartRec + 1, intStartRec + intRecPerPage)SetRecordLabel()Else'reset back to start the record indexintStartRec += intRecPerPageEnd IfEnd SubPrivate Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.ClickintRecPerPage = NumericUpDown1.ValueintStartRec += intRecPerPage'check if the start index recordnya equal to the total number of records'or greater than the total record'it means that no longer exists recordnya (stop Next!!)If intStartRec < taProduct.GetTotalRecord() ThenGetData(intStartRec + 1, intStartRec + intRecPerPage)SetRecordLabel()Else'reset back to start the record indexintStartRec -= intRecPerPageEnd IfEnd SubPrivate Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.ClickintRecPerPage = NumericUpDown1.ValueintTotalRec = taProduct.GetTotalRecord()intStartRec = intTotalRec - intRecPerPageGetData(intStartRec, intTotalRec)SetRecordLabel()End SubEnd Class
- After that we will add a function to calculate the total number of records that exist in that table by using Partial Class. We will extend the TableAdapter class that has been generated at the top.
12345678910111213141516171819Namespace AdventureWorksDataSetTableAdaptersPartial Public Class GetProductPagingTableAdapterPrivate intTotalRec As IntegerPublic Function GetTotalRecord() As IntegerUsing sqlCmd As New SqlCommandUsing sqlConn As New SqlConnection("Database=AdventureWorks;Server=.SqlDev2k5;Integrated Security=SSPI")sqlCmd.CommandType = CommandType.TextsqlCmd.Connection = sqlConnsqlCmd.CommandText = "Select Count (*) From Production.Product"If sqlConn.State ConnectionState.Open Then sqlConn.Open()intTotalRec = Convert.ToInt32(sqlCmd.ExecuteScalar)Return intTotalRecsqlConn.Close()End UsingEnd UsingEnd FunctionEnd ClassEnd Namespace
Download source code :
1 |
http://newbiebanget.fileave.com/PagingDenganTA.rar |
Here paging technique using Data Adapter in VB.Net:
- You create a Windows application project templates
- Add textbox control, numericupdown, buttons, the GridView, the label
- Add code like the following:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788Imports System.Data.SqlClientPublic Class Form1Private intStartRec, intRecPerPage, intTotalRec As IntegerPrivate Function GetTotalRecord() As IntegerUsing sqlCmd As New SqlCommandUsing sqlConn As New SqlConnection("Database=Northwind;Server=localhost;Integrated Security=SSPI")sqlCmd.CommandType = CommandType.TextsqlCmd.Connection = sqlConnsqlCmd.CommandText = "Select Count(*) From Customers"If sqlConn.State ConnectionState.Open Then sqlConn.Open()intTotalRec = Convert.ToInt32(sqlCmd.ExecuteScalar)Return intTotalRecsqlConn.Close()End UsingEnd UsingEnd FunctionPrivate Sub GetData(ByVal startRecord As Integer, ByVal maxRecord As Integer)Using sqlConn As New SqlConnection("Database=Northwind;Server=localhost;Integrated Security=SSPI")Using sqlDa As New SqlDataAdapter("Select CustomerID,CompanyName From Customers", sqlConn)Dim dsCustomers As New DataSetsqlDa.Fill(dsCustomers, startRecord, maxRecord, "dtCustomers")DataGridView1.DataSource = dsCustomers.Tables("dtCustomers")End UsingEnd UsingEnd SubPrivate Sub SetRecordLabel()Label2.Text = "Start Index Record : " + intStartRec.ToString + _" From total record : " + intTotalRec.ToStringEnd SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadintRecPerPage = NumericUpDown1.Value '//jumlah record per pageintStartRec = 0 '//index record awalGetData(intStartRec, intRecPerPage)intTotalRec = GetTotalRecord()SetRecordLabel()End SubPrivate Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.ClickintRecPerPage = NumericUpDown1.ValueintStartRec = 0GetData(intStartRec, intRecPerPage)SetRecordLabel()End SubPrivate Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.ClickintRecPerPage = NumericUpDown1.ValueintStartRec -= intRecPerPage'cek jika start index recordnya = 0 ThenGetData(intStartRec, intRecPerPage)SetRecordLabel()Else'reset kembali start record indexnyaintStartRec += intRecPerPageEnd IfEnd SubPrivate Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.ClickintRecPerPage = NumericUpDown1.ValueintStartRec += intRecPerPage'cek jika start index recordnya sama dengan jumlah record total'atau lebih besar dari total record'maka berarti recordnya sudah tidak ada lagi (stop Next!!)If intStartRec < GetTotalRecord() ThenGetData(intStartRec, intRecPerPage)SetRecordLabel()Else'reset kembali start record indexnyaintStartRec -= intRecPerPageEnd IfEnd SubPrivate Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.ClickintRecPerPage = NumericUpDown1.ValueintTotalRec = GetTotalRecord()intStartRec = intTotalRec - intRecPerPageGetData(intStartRec, intRecPerPage)SetRecordLabel()End SubEnd ClassSource code:
1http://newbiebanget.fileave.com/PagingDenganDA.rar
source: newbiebanget