在ASP+中访问数据库

In an ASP+ page ,databases can be accessed by employing ADO+ objects. In order to retrieve values from
databases through ADO+ we have to follow certain procedures which are different from those employed in ADO.

The article throws Light on Various steps involved in using ADO+ in an ASP page for retrieving the
database values for an access database.

The datagrid control provided with ASP+ is used for displaying the values retrieved from the database.

First include the language in which the asp+ page will be coded. This is specified by using Page directive
as shown below. In this example Let us use 'VB' as The language for coding ASP+ Page. The above thing can
be achieved by using the following page directive as shown below.

1@ Page Language="VB"

Next Import the namespaces required for creating the Datasetcommand object and Other ADO objects such as
connection , dataset. This can be done by the following asp+ statements

1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.ADO" 

Next create the script required for accessing the database. In this example let us use a server side
submit control whose onClick triggers The script given below

 1<script language="VB" runat="server">   
 2Sub subbtn_OnClick(Sender As Object, E As EventArgs)   
 3Dim connobj As ADOConnection   
 4Dim dtsCmdobj As ADODataSetCommand   
 5Dim dtSobj As DataSet   
 6Dim str_sql As String   
 7dim conn_str As String   
 8str_sql = "select * from table_name"   
 9conn_str = "DBQ=" & Server.MapPath("database.mdb") &_   
10";Driver={Microsoft Access Driver (*.mdb)};" 
11
12connobj = New ADOConnection(conn_str)   
13dtsCmdobj = New ADODataSetCommand(str_sql, connobj)   
14dtSobj = New DataSet()   
15dtsCmdob.FillDataSet(dtsobj, "table_name")   
16dtgrid.DataSource = dtsobj.Tables("table_name").DefaultView   
17dtgrid.DataBind()   
18End Sub   
19</script>

Finally place the following html in the asp+ code

1<html>
2<body>
3<form action="db.aspx" method="post" runat="server">
4<asp:button id="subbtn" onclick="subbtn_OnClick" runat="server" text="Run Query" type="submit"></asp:button>
5</form>
6<asp:datagrid headerstyle-font-bold="True" id="dtgrid" maintainviewstate="false" runat="server" tooltip="data Grid provided with asp+"></asp:datagrid>
7</body>
8</html>

Thats it! The datagrid control in ASP+ page is filled up with the values retrieved from the access
database (database.mdb)

Gopikrishna S
Gopikrishna is a specialist in ASP, MTS ,JavaScript , Vbscript, Windows DNA and VBCom . He is currently
associated with Silverline Technologies Limited Chennai.

Published At
Categories with Web编程
Tagged with
comments powered by Disqus