asp.net connect to an Access database

Source: Internet
Author: User
Tags connect access database
access|asp.net| Data | database <%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.OleDb"%>
<script laguage= "VB" runat= "Server" >
Dim MyConnection as OleDbConnection
Dim MyCommand as OleDbCommand
Sub Page_Load (sender as object,e as EventArgs)

' 1. Connecting to the database
Dim dbname As String
Dbname=server.mappath ("Authors.mdb")
MyConnection = New OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &dbname)
Myconnection.open ()
la1.text= "Connection opened!"



' 2. Add a record
mycommand = New OleDbCommand ("Insert into Authors (authors,country) Values (' Simson ', ' USA ')", MyConnection)
Mycommand.executenonquery ()
la2.text= "New record inserted!"



' 3 Update data (Access)
mycommand = New OleDbCommand ("UPDATE Authors SET authors= ' Bennett ' WHERE Authors = ' Simson '", MyConnection)
Mycommand.executenonquery ()
la3.text= "Record updated!"



' 4 Delete data (access)
mycommand = New OleDbCommand ("DELETE from Authors WHERE Authors = ' David '", MyConnection)
Mycommand.executenonquery ()
la4.text= "Record deleted!"



' 5 Display data using Dategrid
mycommand = New OleDbCommand ("SELECT * from Authors", MyConnection)
Mydatagrid.datasource=mycommand.executereader ()
Mydatagrid.databind ()



End Sub
</script>
<body>
<asp:label id= "LA1" runat= "Server"/><br>
<asp:label id= "LA2" runat= "Server"/><br>
<asp:label id= "LA3" runat= "Server"/><br>
<asp:label id= "LA4" runat= "Server"/><br>
<asp:datagrid id= "Mydatagrid" runat= "Server"
Bordercolor= "BLACK"
Borderwidth= "1"
Gridlines= "Both"
Cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
Font-size= "10pt"
Headerstyle-backcolor= "#aaaadd"
Alternatingitemstyle-backcolor= "#eeeeee"
>
</asp:DataGrid>

</body>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.