Knowledges Base
www.shareknowledges.com
A Web Site To Share Knowledges With You
Home
Art and Entertainment
Around The World
Business & Finance
Dot Net Knowledges Base
Dota Allstars
Foreign Exchange (Forex)
Gaming
Products Review & Specs
Programming Articles
Technology
Usefull Links
Link Exchange
Health Beauty Fitness
My Links
San Pian Ye
HuaChao & June
Flash Games
Buy Direct From China
Tips ABC
Dota Guide
Dota Tips
China Resource
Reviews Centre
SK Reviews
Reviews & Tips
Programming Articles
Connecting to MS Access in ASP.NET without a DSN
This script demonstrates connecting to an Access database in ASP.NET without a DSN. It is written in VB. The script will make the connection and then output all the rows in the specified table to an HTML table.
Public Sub Page_Load(Source As Object, E As EventArgs)
This function is called when the page loads.
Dim ConnectionString As String
ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & dbpath & ";"
Declares a variable and creates the connection string.
Dim dbpath As String
dbpath = "C:path odatabasedb est.mdb"
Declares a variable and sets it to the database path.
Dim QuerySQL As String
QuerySQL = "SELECT * FROM table"
Declares a variable and sets it to the query string.
Dim objConn As New OleDbConnection(ConnectionString)
Creates the connection object from the connection string.
Dim objCmd As OleDbCommand
objCmd = New OleDbCommand
objCmd.CommandText = QuerySQL
objCmd.Connection = objConn
Sets the query object.
Try
objConn.Open()
Opens the connection to the database.
dg1.DataSource = objCmd.ExecuteReader()
dg1.DataBind()
Executes the query and gives the results to the data grid "dg1".
Catch Err As Exception
ltlError.Text = Err.ToString()
Catch any errors in the connection
objConn.Close()
Close the database connection.
End Try
<html>
<head>
<title>Connecting to Microsoft Access Database</title>
</head>
<body>
<form runat="server">
<h3>Connecting to Microsoft Access Database</h3>
<h3>
<asp:DataGrid id="dg1" runat="server"></asp:DataGrid>
</h3>
<p>
<asp:Literal id="ltlError" runat="server"></asp:Literal>
</p>
</form>
</body>
</html>
Prints out the results of the database query from the data grid in HTML.
--Here is the full script, the filename should be: access-dsnless-vb.aspx--
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Page Language="VB" %>
<script runat="server">
Public Sub Page_Load(Source As Object, E As EventArgs)
Dim ConnectionString As String
Dim dbpath As String
dbpath = "C:path odatabasedb est.mdb"
ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & dbpath & ";"
Dim QuerySQL As String
QuerySQL = "SELECT * FROM table"
Dim objConn As New OleDbConnection(ConnectionString)
Dim objCmd As OleDbCommand
objCmd = New OleDbCommand
objCmd.CommandText = QuerySQL
objCmd.Connection = objConn
Try
objConn.Open()
dg1.DataSource = objCmd.ExecuteReader()
dg1.DataBind()
Catch Err As Exception
ltlError.Text = Err.ToString()
Finally
objConn.Close()
End Try
End Sub
</script>
<html>
<head>
<title>Connecting to Microsoft Access Database</title>
</head>
<body>
<form runat="server">
<h3>Connecting to Microsoft Access Database</h3>
<h3>
<asp:DataGrid id="dg1" runat="server"></asp:DataGrid>
</h3>
<p>
<asp:Literal id="ltlError" runat="server"></asp:Literal>
</p>
</form>
</body>
</html>
--End Script--
1/5/2008 2:14:01 AM
Category
Database Connection Strings
Comments 0
People who read this, also read...
Connecting to MSSQL in ASP.NET with a DSN
Connecting to MySQL in ASP.NET without a DSN
Connecting to MSSQL in Perl with a DSN
Connecting to MSSQL in Perl without a DSN
Connecting to MySQL in Perl with a DSN
Back
© 2007 www.shareknowledges.com
HuaChao & June
San Pian Ye
Share Knowledges