ASP. NET/perl. Net)

Source: Internet
Author: User

ASP. NET/perl. NET database access example
One of the features of. net Framework is its ability to handle multiple ages. third party compiler vendors can create and implement a compiler targeted for. net runtime. in fact, over the course of the next year, you can see some 28 languages ages ported to. net runtime. this will allow companies with a huge codebase in a "non-Microsoft language" to continue building onto their investment.

 

Note: All of the software required to create and run ASP. NET pages built using Perl. Net can be downloaded here.

Here is the code for the whole page (We will dissect and explain each bit of the code throughout the rest of the article ):

<% @ Page
Language = "Perl" %>
<SCRIPT runat = "server">

Use namespace "system: Data ";
Use namespace "system: Data: sqlclient ";

= For interface
Protected override void onload (system. eventargs E );
= Cut

Sub onload {
My ($ this, $ e) = @_;

My $ myconnection = sqlconnection-> New ("Data Source = (local); trusted_connection = yes; initial
Catalog = pubs ");
My $ mycommand = sqlcommand-> New ("select * from publishers", $ myconnection );

$ Myconnection-> open ();

$ This-> {mydatagrid} {datasource} = $ mycommand-> executereader (perlnet: Enum
("Commandbehavior. closeconnection "));
$ This-> {mydatagrid}-> databind;
}

</SCRIPT>
<HTML>
<Body>
<Form runat = "server">
<Asp: DataGrid id = "mydatagrid" runat = "server"/>
</Form>
</Body>
</Html>

We need to set the language attribute (in our declaration) equal to 'perl '. we do this so it is clear to. net runtime exactly which compiler it must invoke. the following code accomplishes this task:

<% @ Page
Language = "Perl" %>

Now, we need place all of our Perl. net code inside <SCRIPT runat = "server" </SCRIPT> blocks just as we wocould if we were using VB. net or C #. here is where you will notice one (there are more) Difference between VB. net/C #/JScript. net and Perl. net pages. in Perl. net, rather than using import declarations outside of the <SCRIPT> blocks the code that imports the required namespaces is placed inside the <SCRIPT> blocks. this Code uses the following syntax:

Use
Namespace "system: Data ";
Use namespace "system: Data: sqlclient ";

We want code to execute each time the page is loaded so we must explicitly define our onload event in order for the page to recognize it. The following code accomplishes this task:

= For interface
Protected override void onload (system. eventargs E );
= Cut

Note: All events in the page must be declared using code similar to that above. also, all interface declarations must be flush left on the page (ie '= for interface' must start in the left most column on the page ).

Next, we declare our onload event. The following Code declares the onload event and places any arguments into the scalar variables $ this and $ E:

Sub onload {
My ($ this, $ e) = @_;

So, we have declared our onload event and now we need to write the code to be executed in the event. the code in our example connects to a SQL Server database and retrieves information in the form of a datareader. first, we will examine the code that connects to SQL Server and selects the desired information.

My $ myconnection = sqlconnection-> New ("Data
Source = (local); trusted_connection = yes; initial catalog = pubs ");
My $ mycommand = sqlcommand-> New ("select * from publishers", $ myconnection );

$ Myconnection-> open ();

'My declares variables to be local to the enclosing block-in our case the onload event. in Perl. net, we call an object's methods using this syntax: Object-> methodname.

OK, we have created the necessary objects and opened our connection to SQL Server. the variable $ this contains a reference to the our page. anytime we want to reference controls on the page (ie mydatagrid) We use $ this. so, mydatagrid is a property of the page-all properties are referenced using this syntax: {propertyname }.

$ This-> {mydatagrid} {datasource}
= $ Mycommand-> executereader (perlnet: Enum ("commandbehavior. closeconnection "));
$ This-> {mydatagrid}-> databind;

The code above also demonstrates how we use enumerations in Perl. net. enumerations are referenced using the following syntax: perlnet: Enum ("Name of enumeration "). the rest of the Code (HTML and DataGrid Declaration) in the page is no different than in a ASP. NET page written in VB. net or C #.

This example is just a small taste of what we can do with Perl. net! Hopefully, this article will jumpstart all you Perl developers into trying Perl. Net/ASP. NET!

Related Article

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.