With the development of network technology and the popularity of the Internet, Browser/Server has become the mainstream in software development. When developing an ERP system, the author adopted the B/S software model. The specific architecture is SQL Server + IIS + IE. The web page uses Active Server Page file. Since the system involves a large amount of data operations and queries, if it is purely written in asp script language, it will inevitably lead to low efficiency. In order to improve the overall efficiency and security of the system, the author uses ASP components to replace the ASP script language.
Due to Delphi's powerful functions and extremely high efficiency in developing database application systems, the author most commonly uses Delphi 5.0 to develop ASP components (of course, Visual Basic or VC++ can also be used to develop ASP components). Delphi itself is widely used on the Internet and InternetExPRess The two component panels provide numerous components that can directly generate Web pages, but these components lack the common paging function for data display in web pages. As we all know, ASP establishes a RecordSet object by establishing an ADO connection to the database, and then uses the AbsolutePage of the RecordSet for page positioning. In Delphi 5.0, the ADO component has been provided to encapsulate Microsoft's ADO library, so it also has the page positioning function. Next, the author will develop a general ASP component that displays paging Web pages step by step.
The first step: Create a new Activex Library, named PadoPage, and then create a new Active Server Object Class, named AdoPage, that is, an ASP component named AdoPage is created, and the file is named Adopage.pas.
Step 2: Open the Type Library, create a new method Get_Page, and then add a parameter Pconnandsgl to Get_Page to pass the database connection statement and SQL statement. The parameter selection is of BSTR type.
Step 3: Create a new DataModule, put in the Adoconnection component and AdoQuery component, and name the Data Module AdoDataModule. Since the method Get_Page in the newly created component needs to obtain data from DataModule, you need to add AdoDataModule to the Uses clause of Adopage.pas, then declare a variable fadodm of the data module, and add the two methods Initialize and Destroy at the same time, so that Generate data modules in ASP group work. The specific code of Adopage.pas is as follows:
unit Adopage;
interface
uses
ComObj, SysUtils, Classes, ActiveX, AspTlb, Pbasedata_TLB, StdVcl, AdoDataModule;
//Add AdoDataModule to the USE clause
type
T Adopage = class(TASPObject, Ibasedata)
private
fadodm:TAdoDataModuleform;
protected
procedure OnEndPage; safecall;
procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
procedure get_page(const pconnandsql: WideString); safecall;
public
procedure initialize;override;
destructor destroy;override;
end;
implementation
uses ComServ,forms;
destructor Tadopage.destroy;
begin
inherited;
fadodm.Destroy;
end;
procedure Tadopage.initialize;
begin
inherited;
fadodm:=tadodmform.Create(forms.application);
end;
Step 4: Establish a general paging method for displaying data, get_page. The specific code is as follows:
procedure Tadopage.get_page(const pconnandsql: WideString);
var i,j,n:integer;
connstr,sqlstr:widestring;
rs:_recordset;
cur_url:widestring;
page_no:integer;
begin
//First, take out the connection string and SQL statement from the passed parameters.
pconnandsql:=uppercase(pconnandsql);
i:=pos('CONNSTR',pcnnandsql);
j:=pos('SQLSTR',pcnnandsql);
if i=0 or j=0 then
begin
response.write('Database connection string or SQL statement error!');
abort;
end;
for n:=I+8 to j-1 do
connstr:=connstr+pcnnandsql[n];
for n:=j+7 to length(pcnnandsql) do
sqlstr:=sqlstr+pcnnandsql[n];
//Assign the obtained connection string and SQL statement to ADOconnection and ADOQuery respectively
fadodm.adoconnection1.connstring:=connstr;
fadodm.adoquery1.sql.add(sqlstr);
//The following is the process of opening the database and performing paging
try
fadodm.adoquery1.open;
//Open the database
rs:=fadodm.adoquery1.recordset;
//Get the URL and page number of the currently opened page
try
if request.servervariable['url'].count>0 then
cur_url:= request.servervariable.item['url'];
if request.querystring['page_no'].count>0 then
page_no:=request.querystring.item['page_no']
else
page_no:=1;
except
end;
rs.pagesize:=20;
//Set 20 lines per page
rs.AbsolutePage:=page_no;
//Page positioning
response.write('total'+inttostr(rs.pagecount)+'page& ');
response.write('th'+inttostr(page_no)+'page& ');
//Create a hyperlink for each page number
for i:=1 to rs.pagecount do
response.write('<a href='+cur_url+'?page_no='+inttostr(i)+'>'
+inttostr(i)+'</a>');
//Data records are displayed in tables
response.write('<table>');
//Get the table title
response.write('<tr>');
for I:=0 to fadodm.adoquery1.fields.count-1 do
response.write('<td>'+fadodm.adoquery1.fields[i].fieldname+'</td>');
response.write('</tr>');
j:=1
with fadodm.adoquery1 do
while (not eof) and j<=rs.pagesize do
begin
response.write('<tr>');
//Get table content
for i:=1 to fields.count do
response.write('<td>'+fields[i].asstring+'</td>');
response.write('</tr>');
next;
end;
response.write('</table>');
fadodm.adoquery1.close;
except
response.write('Data error!');
end;
end;
The above is the process of obtaining universal paging data. It should be noted that some functions will cause errors during compilation. Just add sysutils, classes and adodb units to the USES clause.
Step 5: Compile and register the adopage component, which can be called in the ASP code. The calling example is as follows:
<%
dim webpageobj
set webpageobj=server.createobject(padopage.adopage)
webpageobj.get_page(conn=provider=SQLOLEDB.1;presist security info=false;
user id=sa;initial catalog=sale_data;data source=(local),
sqlstr=selectfrom customer)
%>
Through the above steps, we have successfully developed an ASP component with paging function using Delphi.