Migration from ASP to asp+--initial considerations (ii)

Source: Internet
Author: User
Asp+ uses the same idea as creating a universal connection in a single module, we created a pagelet. Pagelet enables developers to create pseudo controls, which can be used to display properties, methods, and events as well as objects. Our solution is to create a pagelet that displays the Connecttodb method, which returns a ADOConnection object (slightly different from the syntax of ADO). We use the following code to create the Pagelet and save it as a connect.aspc (the extension indicates that it is a pagelet)

<%@ Import namespace= "System.Data"%>

<%@ Import namespace= "System.Data.ADO"%>

< script language= "VB" runat= "Server" >

Public Function Connecttodb () as ADOConnection

Connecttodb = New adoconnection ("Dsn=evilledsn")

End Function

</script>

You will notice that we have imported two namespaces, namely System.Data and System.Data.ADO. In order to use the ado+ Managed provider these namespaces are necessary. Many people ask me a lot of questions about this, because most of the examples currently use SQL Managed Provider based on Microsoft SQL Server 2000 databases. For non-SQL Server databases, asp+ can use ado+ Managed Provider, which is roughly the same as the way you use ADO in your program. importing namespaces and creating references in VB Programs (Reference) are similar.

The use of Pagelet allows me to maintain the current site plan, and once the connection is created in the module, we can reuse the module wherever it is needed. This means that we need to replace the include file instruction with an instance of Pagelet, and then call the Connecttodb method when we need a database connection. In pages that require the use of pagelet, we first have to register (register) with the page using the asp+ directive. In the instructions, we define tagprefix,tagname, and source (SRC). Similar to the include file in ASP, TagPrefix and tagname are used where we place pagelet.

<%@ Register tagprefix= "Seven" tagname= "Connect"

Src= "_INCLUDES/CONNECT.ASPC"%>

The method for placing the pagelet we just registered is similar to the asp+ server control:

< [tagprefix]:[tagname] Id=mypagelet runat=server/>

Like what:

< Seven:connect id=connect Runat=server/>

In ado+, the concept of a Recordset (recordset) is replaced by a combination of datasets and DataViews. We will discuss these two concepts in a moment. Let's start with a look at how the code changes after the Default.asp page migrates to Default.aspx (asp+ page suffix). First look at the Default.asp:

<!--#include file= "_includes/connect.inc"-->

<%

Dim cneville_db, rsupcoming, strsqlupcoming

Set rsupcoming = Server.CreateObject ("ADODB. Recordset ")

strsqlupcoming = "SELECT Top 2" & _

"Classes.title, sessions.session_id," & _

"Sessions.special, Classes.description" & _

"From Classes INNER JOIN Sessions on" & _

"classes.class_id = Sessions.classid" & _

"WHERE ((sessions.date) >date ())" & _

"ORDER by Sessions.date"

Rsupcoming.open strsqlupcoming,cneville_db

%>

In the asp+ became the Default.aspx:

<%@ Import namespace= "System.Data"%>

<%@ Import namespace= "System.Data.ADO"%>

<%@ Register tagprefix= "Seven" tagname= "Connect" &_

Src= "_INCLUDES/CONNECT.ASPC"%>

< script language= "VB" runat=server>

Sub Page_Load (Source as Object, E as EventArgs)

Dim dscupcoming as Adodatasetcommand

Dim dsupcoming as New DataSet

Dim strSQL as String

strSQL = "Select top 2 Classes.title," & _

"Sessions.session_id, Classes.description" & _

"From Classes INNER JOIN Sessions on" & _

"classes.class_id = Sessions.classid" & _

"WHERE ((sessions.date) >date ())" & _

"ORDER by Sessions.date"

dscupcoming = New Adodatasetcommand (strSQL, Connect.connecttodb ())

Dscupcoming.filldataset (dsupcoming, "upcoming")

End Sub

</script>

The core of data access, Ansi-sql statement after conversion nothing

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.