Connect to a Database ( and read it )

Tutorials and code examples using ASP and ASP.Net
7 posts Page 1 of 1
Contributors
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

Hello,
I'm learning this @ school so why I won't share it?
:)

First of all make a database: Named dbPersons ( I used acces 2010 ):
Image
Then save the table as "tblPersons". Then don't just save the db but publish ( if using acces 2007 or higher ):
Image

Then go to you're microsoft Visual Web Developer and Create a new aspx project. I named it "20110304-Codenstuff-aspxdatareader"
then u search you're db file " dbPersons" and drag it in App_Data:
Image
U can also see I deleted some files. But that isn't necesairy. Then we are going to add a page named "Datareader.aspx":
Image

So now the coding can begin !
Lets start with getting the messy stuff out of the page! Delete the first sententce and replace it with:
Code: Select all
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 End Sub
</script>
So u'll gain this:
Image
Then the connection!
Code: Select all
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                    Server.MapPath("App_Data\dbPersons.mdb")
        'Specifie the Database type and path
        Dim strSQL As String = "SELECT * FROM tblPersons"
        'SQL Query
        Dim cn As New OleDbConnection(strConn)
        'Make CN ( Not open )
        Try
            cn.Open()
            'Open CN
            Dim cm As New OleDbCommand(strSQL, cn)
            'make command from SQL
            Dim dr As OleDbDataReader = cm.ExecuteReader()
            'make Reader
            If dr.HasRows() Then
                ' check if there are rows in the Db
                While dr.Read()
                    'He is checking every row
                    Label1.Text = Label1.Text & dr("Id").ToString() & " " & _
                            dr("Name").ToString() & " " & _
                            dr("Prename").ToString() & " " & _
                            dr("E-Mail").ToString() & " " & _
              dr("Country").ToString() & "<br>"
                End While
            Else
                'No rows are found. Add some to the DB
                Label1.Text = "No rows found."
            End If
        Catch ex As Exception
            Trace.Warn(ex.Message)
            'Problem probably you have a wrong path or SQL
            Label1.Text = "Problem with database."
        Finally
            'Close connection ALWAYS
            cn.Close()
        End Try
This will result:
Image

The Source files Will cost some credits because I explained all ;)
RAR:
20110304-Codenstuff-aspxdatareader.rar
ZIP:
20110304-Codenstuff-aspxdatareader.zip
So this is it?

Thx :) And rep me if helped plox :)

Credits:
My teacher who taught me and my class.
You do not have the required permissions to view the files attached to this post.
Last edited by Kieken72 on Sat Mar 05, 2011 3:01 pm, edited 2 times in total.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Nice tutorial, I have been waiting for this for weeks now. Finally someone shows me how to do it!
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Very nice and detailed tutorial. +rep
btw. its called "taught"
Credits:
My teacher who learnd it to me and my class.
should be
Credits:
My teacher who taught me and my class.
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

CodexVideos wrote:
Very nice and detailed tutorial. +rep
btw. its called "taught"
Credits:
My teacher who learnd it to me and my class.
should be
Credits:
My teacher who taught me and my class.
Sorry ^^
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

I'll will be making more to ;)
User avatar
marve25
VIP - Donator
VIP - Donator
Posts: 40
Joined: Sun Sep 20, 2009 3:47 pm

can i connect to a MySQL server with it?
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

You would need to use the MySQL connector instead of an Access database, but the syntax would be very similar.
7 posts Page 1 of 1
Return to “Tutorials”