欢迎光临
我们一直在努力

ASP.NET/Perl.NET 数据库访问例子-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

asp.net/perl.net database access example
one of the features of the .net framework is its 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 see some 28 languages ported to the .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 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 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 objects 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!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET/Perl.NET 数据库访问例子-.NET教程,Asp.Net开发
分享到: 更多 (0)