PDA

View Full Version : Database Connection Sample


bruce
02-10-2003, 10:45 AM
As requested by some users, i am providing a very simple database connection sample.

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.IO" %>
<script runat="server">

Sub Page_Load


Dim conn as OleDbConnection
Dim cmd as OleDbCommand
Dim dbReader as OleDbDataReader
Dim result as string

' Create Connection using OLEDB
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("\test.mdb"))
Dim strSQL as string = "select * from customers"

conn.open()
cmd = new OleDbcommand(strSQL,conn)
dbReader = cmd.executereader

' Bind result to repeater control
repeater1.datasource = dbReader
repeater1.databind()

' Close Connections
dbReader.close()
conn.close()


End Sub

</script>
<html>
<head>
</head>
[b]
<form runat="server">
<h1>Simple Application to retrieve &amp; display data using DataReader Control
</h1>
<p>
This page contains a simple application to retrieve data from an Access database and
displaying it using Datareader &amp; repeater control. (<a href="retrievedata1_source.htm">source
code</a>)
</p>
<p>
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="0">
<tr bgcolor="#C0FFC0">
<th>
Name</th>
<th>
Address</th>
<th>
City</th>
<th>
State</th>
<th>
Zip</th>
<th>
Country</th>
</tr>
</HeaderTemplate>
<itemTemplate>
<tr>
<td>
<%# container.dataItem("name") %></td>
<td>
<%# container.dataItem("address") %></td>
<td>
<%# container.dataItem("city") %></td>
<td>
<%# container.dataItem("state") %></td>
<td>
<%# container.dataItem("zip") %></td>
<td>
<%# container.dataItem("country") %></td>
</tr>
</itemtemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

</p>
</form>
</body>
</html>

pupu79
02-16-2003, 05:21 AM
The statement "Dim conn as OleDbConnection" causes error in my page.

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/new/db_test.asp, line 10

Dim Conn as OleDbConnection
---------^

bruce
02-17-2003, 11:22 AM
You need to import the

system.data.oledb namespace


[b]quote:Originally posted by pupu79

The statement "Dim conn as OleDbConnection" causes error in my page.

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/new/db_test.asp, line 10

Dim Conn as OleDbConnection
---------^

</blockquote id="quote"></font id="quote">