Basically I want to talk about the focus is how to use, because in fact, the paging stored process online A lot, if you are too lazy to find, then you can directly use the following I have tested, and through modification, online evaluation are relatively high paging stored procedures.
The main advantages of this paging are as follows:
1, the large capacity of the data paging, my test data is 520W.
2, I combine Aspnetpager control, make it more convenient to use.
3, in order to clear structure, practical 3-storey.
4, safety, you can rest assured that use it. The problem with SQL injection here you can rest assured. There are articles on the web saying that there is a problem with SQL injection as long as the stored procedure is spliced in SQL, and that the injection is tested directly in SQL Query Analyzer. In fact, there is no SQL injection problem with stored procedures and parameterized submission statements. Because it has a replacement process when it comes into the database.
preparatory work:
1, directly using a DB Library, data access layer base class, use it to return a DataSet object, when used we only need similar to the following statements can return a DataSet object.
Sosuo8. DBUtility.DbHelperSQL.RunProcedure ("pagination", Parameter, "userinfo");
Pagination is the stored procedure I find on the Internet, I have modified, mainly add output total record number. The total number of records here is also deliberately said, generally we are using a statement similar to the following:
Copy Code code as follows:
Sosuo8. DBUtility.DbHelperSQL.RunProcedure ("pagination", Parameter, "userinfo");
Pagination is the stored procedure I find on the Internet, I have modified, mainly add output total record number. The total number of records here is also deliberately said, generally we are using a statement similar to the following:
Copy Code code as follows:
Select COUNT (*) from Sosuo8data
Here I have to say two, online users say that using count (a column), such as Count (UserName) will be faster than COUNT (*) is not correct, if the wrong column, then no faster than COUNT (*). and COUNT (*) will automatically help us find the column that can achieve the fastest statistics, and in fact in use, is usually our primary key Id,count (ID) is the fastest.
2, the establishment of database data_test, the establishment of two tables:
(1), userinfo This table is used to put data.
Copy Code code as follows:
CREATE TABLE [dbo]. [UserInfo] (
[ID] [int] IDENTITY (1,1) not NULL,
[UserName] [NCHAR] (m) COLLATE chinese_prc_ci_as NULL,
[Userwebname] [NCHAR] (m) COLLATE chinese_prc_ci_as NULL,
[CreateDate] [DateTime] NULL CONSTRAINT [df_userinfo_createdate] DEFAULT (getdate ()),
CONSTRAINT [Pk_userinfo] PRIMARY KEY CLUSTERED
(
[ID] ASC
) with (Ignore_dup_key = out) on [PRIMARY]
) on [PRIMARY]
The ID is incremented and is an aggregated index. OK? Start adding 520W Records to the database:
Copy Code code as follows:
SET Identity_insert userinfo on--identity can be inserted into a self-incrementing column
DECLARE @count int
DECLARE @date datetime
Set @count =1
Set @date = ' 2009-4-5 00:00:00 '
While @count <=5200000
Begin
Insert into UserInfo (id,username,userwebname,createdate) VALUES (@count, ' O ' nan ', ' sosuo8.com ', @date)
Set @count = @count +1
Set @date = @date + ' 00:00:01 '-plus one second, to avoid duplication, otherwise it will result in inaccurate pagination, the sorted field should not be too many duplicate values
End
SET Identity_insert userinfo off
If your computer configuration is more general, do not try, or you may become a machine, if not a machine, that patient wait, insert so many records need a little time.
(2) The TMP table is used to store
total number of records without search criteria。 Here I would like to say, why need a table to store the total number of records, the total number of records you can be in the background every once in a while to update, the latest total record number. Otherwise, 520W of records you have to use COUNT (ID) to spend a lot of time.
The statement to build the table is as follows:
Copy Code code as follows:
CREATE TABLE [dbo]. [TMP] (
[ID] [int] IDENTITY (1,1) not NULL,
[Rowcount_tmp] [INT] Null
[Table_tmp] [varchar] (255) COLLATE chinese_prc_ci_as NULL,
CONSTRAINT [pk_tmp] PRIMARY KEY CLUSTERED
(
[ID] ASC
) with (Ignore_dup_key = out) on [PRIMARY]
) on [PRIMARY]
Here's the reason why you just get the total number of rows in the table, not all the pagination in the stored procedure. Because we're going to use the Aspnetpager control, which we only need to pass in 3 values, the function is defined as follows:
Copy Code code as follows:
Public DataSet getlist (int PageIndex, string strwhere,ref int rowcount)
PageIndex: Current page, corresponding to the CurrentPageIndex in Aspnetpager
Strwhere: Search terms, this article will not focus on the search section. So the code uses string. Empty, large-capacity data search that requires another article to explain.
ROWCOUNT: The total number of records returned by the stored procedure.
current 1/2 page 1 2 Next read the full text