Asp.net/perl.net Database Access Example

Source: Internet
Author: User
asp.net|perl| Access | data | Databases asp.net/perl.net database access Example
One of the features of the. NET Framework is it ability to handle multiple languages. Third party compiler vendors can create and implement a compiler targeted for the. NET Runtime. In fact, over the course of the "next year", you can expect to the some languages to the. NET ported. This would allow companies with the huge codebase in a "Non-microsoft language" to continue building onto their.

Note:all of the software required to create and run asp.net pages built using Perl.net can is downloaded here.

This is the code for the whole page (we'll dissect and explain each bit of the code throughout the rest of the article):

<%@ Page
Language= "Perl"%>
<script runat= "Server" >

Use namespace "System::D ata";
Use namespace "System::D ata::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>
<body>
<form runat= "Server" >
<asp:datagrid id= "Mydatagrid" runat= "Server"/>
</form>
</body>

We need to set the Language attributes (in our Declaration) equal to ' Perl '. We are clear to the. 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 would if we were using vb.net or C #. Here's where you'll 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 to 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::D ata";
Use namespace "System::D ata::sqlclient";


We want code to execute the "page is loaded" so we must explicitly define our OnLoad event in order for the page T o 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 is declared using code similar to that above. Also, all interface declarations must is flush left on the page (ie ' =for interface ' must ' "start in" 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 we OnLoad event and now we are need to write the "code to" executed in the event. The code in we example connects to a SQL Server database and retrieves information the form of a DataReader. The connects to SQL Server and selects the desired information of the examine.

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 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 are 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 was 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 does with perl.net! Hopefully, this article'll jumpstart all your 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.