SlideShare ist ein Scribd-Unternehmen logo
1 von 3
ASP.NET - Database Connection

ADO.NET is also a part of the .NET Framework. ADO.NET is used to handle data access.
With ADO.NET you can work with databases.



Examples

Database connection - Bind to a Repeater control

Database connection - Bind to a DataList control



What is ADO.NET?

    •   ADO.NET   is a part of the .NET Framework
    •   ADO.NET   consists of a set of classes used to handle data access
    •   ADO.NET   is entirely based on XML
    •   ADO.NET   has, unlike ADO, no Recordset object



Create a Database Connection

We are going to use the Northwind database in our examples.

First, import the quot;System.Data.OleDbquot; namespace. We need this namespace to work with Microsoft
Access and other OLE DB database providers. We will create the connection to the database in the
Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class with a
connection string which identifies the OLE DB provider and the location of the database. Then we
open the database connection:

<%@ Import Namespace=quot;System.Data.OleDbquot; %>
<script runat=quot;serverquot;>
sub Page_Load
dim dbconn
dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=quot; & server.mappath(quot;northwind.mdbquot;))
dbconn.Open()
end sub
</script>

Note: The connection string must be a continuous string without a line break!



Create a Database Command

To specify the records to retrieve from the database, we will create a dbcomm variable as a new
OleDbCommand class. The OleDbCommand class is for issuing SQL queries against database tables:

<%@ Import Namespace=quot;System.Data.OleDbquot; %>
<script runat=quot;serverquot;>
sub Page_Load
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=quot; & server.mappath(quot;northwind.mdbquot;))
dbconn.Open()
sql=quot;SELECT * FROM customersquot;
dbcomm=New OleDbCommand(sql,dbconn)
end sub
</script>


Create a DataReader

The OleDbDataReader class is used to read a stream of records from a data source. A DataReader is
created by calling the ExecuteReader method of the OleDbCommand object:

<%@ Import Namespace=quot;System.Data.OleDbquot; %>
<script runat=quot;serverquot;>
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=quot; & server.mappath(quot;northwind.mdbquot;))
dbconn.Open()
sql=quot;SELECT * FROM customersquot;
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
end sub
</script>


Bind to a Repeater Control

Then we bind the DataReader to a Repeater control:

<%@ Import Namespace=quot;System.Data.OleDbquot; %>
<script runat=quot;serverquot;>
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0;
data source=quot; & server.mappath(quot;northwind.mdbquot;))
dbconn.Open()
sql=quot;SELECT * FROM customersquot;
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:Repeater id=quot;customersquot; runat=quot;serverquot;>
<HeaderTemplate>
<table border=quot;1quot; width=quot;100%quot;>
<tr>
<th>Companyname</th>
<th>Contactname</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem(quot;companynamequot;)%></td>
<td><%#Container.DataItem(quot;contactnamequot;)%></td>
<td><%#Container.DataItem(quot;addressquot;)%></td>
<td><%#Container.DataItem(quot;cityquot;)%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>


Close the Database Connection

Always close both the DataReader and database connection after access to the database is no
longer required:

dbread.Close()
dbconn.Close()

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Data controls ppt
Data controls pptData controls ppt
Data controls ppt
 
Active server pages
Active server pagesActive server pages
Active server pages
 
Active server pages
Active server pagesActive server pages
Active server pages
 
Asp objects
Asp objectsAsp objects
Asp objects
 
Introduction to asp
Introduction to aspIntroduction to asp
Introduction to asp
 
Server Controls of ASP.Net
Server Controls of ASP.NetServer Controls of ASP.Net
Server Controls of ASP.Net
 
mush With Xampp
mush With Xamppmush With Xampp
mush With Xampp
 
Rail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendranRail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendran
 
mule salesforce
mule salesforcemule salesforce
mule salesforce
 
Asp
AspAsp
Asp
 
Tutorial asp.net
Tutorial  asp.netTutorial  asp.net
Tutorial asp.net
 
Ajax and ASP.NET AJAX
Ajax and ASP.NET AJAXAjax and ASP.NET AJAX
Ajax and ASP.NET AJAX
 
Asp.net web page syntax overview
Asp.net web page syntax overviewAsp.net web page syntax overview
Asp.net web page syntax overview
 
Object Oriented Programming with Laravel - Session 6
Object Oriented Programming with Laravel - Session 6Object Oriented Programming with Laravel - Session 6
Object Oriented Programming with Laravel - Session 6
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Introduction ASP
Introduction ASPIntroduction ASP
Introduction ASP
 
2310 b 13
2310 b 132310 b 13
2310 b 13
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance Topics
 
05 Web Services
05 Web Services05 Web Services
05 Web Services
 

Andere mochten auch

Andere mochten auch (8)

Microsoft Word Shortcut Keys
Microsoft Word Shortcut KeysMicrosoft Word Shortcut Keys
Microsoft Word Shortcut Keys
 
.net framework
.net framework.net framework
.net framework
 
There Is Always A Better Way
There Is Always A Better WayThere Is Always A Better Way
There Is Always A Better Way
 
3 Things In Our Life
3 Things In Our Life3 Things In Our Life
3 Things In Our Life
 
Attitude
AttitudeAttitude
Attitude
 
Apple Macintosh Shortcut Keys
Apple Macintosh Shortcut KeysApple Macintosh Shortcut Keys
Apple Macintosh Shortcut Keys
 
Discrete Mathematics - Mathematics For Computer Science
Discrete Mathematics -  Mathematics For Computer ScienceDiscrete Mathematics -  Mathematics For Computer Science
Discrete Mathematics - Mathematics For Computer Science
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 

Ähnlich wie Asp.Net Database

Ähnlich wie Asp.Net Database (20)

Ado.Net
Ado.NetAdo.Net
Ado.Net
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
Ado.net
Ado.netAdo.net
Ado.net
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Introduction to ado
Introduction to adoIntroduction to ado
Introduction to ado
 
Csharp_dotnet_ADO_Net_database_query.pptx
Csharp_dotnet_ADO_Net_database_query.pptxCsharp_dotnet_ADO_Net_database_query.pptx
Csharp_dotnet_ADO_Net_database_query.pptx
 
Marmagna desai
Marmagna desaiMarmagna desai
Marmagna desai
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
 
Ado .net
Ado .netAdo .net
Ado .net
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Unit4
Unit4Unit4
Unit4
 
Ado
AdoAdo
Ado
 
ADO.NET -database connection
ADO.NET -database connectionADO.NET -database connection
ADO.NET -database connection
 
Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net Architecture
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
Ado.net
Ado.netAdo.net
Ado.net
 
Ado dot net complete meterial (1)
Ado dot net complete meterial (1)Ado dot net complete meterial (1)
Ado dot net complete meterial (1)
 
Visual Basic.Net & Ado.Net
Visual Basic.Net & Ado.NetVisual Basic.Net & Ado.Net
Visual Basic.Net & Ado.Net
 

Kürzlich hochgeladen

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Asp.Net Database

  • 1. ASP.NET - Database Connection ADO.NET is also a part of the .NET Framework. ADO.NET is used to handle data access. With ADO.NET you can work with databases. Examples Database connection - Bind to a Repeater control Database connection - Bind to a DataList control What is ADO.NET? • ADO.NET is a part of the .NET Framework • ADO.NET consists of a set of classes used to handle data access • ADO.NET is entirely based on XML • ADO.NET has, unlike ADO, no Recordset object Create a Database Connection We are going to use the Northwind database in our examples. First, import the quot;System.Data.OleDbquot; namespace. We need this namespace to work with Microsoft Access and other OLE DB database providers. We will create the connection to the database in the Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class with a connection string which identifies the OLE DB provider and the location of the database. Then we open the database connection: <%@ Import Namespace=quot;System.Data.OleDbquot; %> <script runat=quot;serverquot;> sub Page_Load dim dbconn dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0; data source=quot; & server.mappath(quot;northwind.mdbquot;)) dbconn.Open() end sub </script> Note: The connection string must be a continuous string without a line break! Create a Database Command To specify the records to retrieve from the database, we will create a dbcomm variable as a new OleDbCommand class. The OleDbCommand class is for issuing SQL queries against database tables: <%@ Import Namespace=quot;System.Data.OleDbquot; %> <script runat=quot;serverquot;> sub Page_Load dim dbconn,sql,dbcomm dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0;
  • 2. data source=quot; & server.mappath(quot;northwind.mdbquot;)) dbconn.Open() sql=quot;SELECT * FROM customersquot; dbcomm=New OleDbCommand(sql,dbconn) end sub </script> Create a DataReader The OleDbDataReader class is used to read a stream of records from a data source. A DataReader is created by calling the ExecuteReader method of the OleDbCommand object: <%@ Import Namespace=quot;System.Data.OleDbquot; %> <script runat=quot;serverquot;> sub Page_Load dim dbconn,sql,dbcomm,dbread dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0; data source=quot; & server.mappath(quot;northwind.mdbquot;)) dbconn.Open() sql=quot;SELECT * FROM customersquot; dbcomm=New OleDbCommand(sql,dbconn) dbread=dbcomm.ExecuteReader() end sub </script> Bind to a Repeater Control Then we bind the DataReader to a Repeater control: <%@ Import Namespace=quot;System.Data.OleDbquot; %> <script runat=quot;serverquot;> sub Page_Load dim dbconn,sql,dbcomm,dbread dbconn=New OleDbConnection(quot;Provider=Microsoft.Jet.OLEDB.4.0; data source=quot; & server.mappath(quot;northwind.mdbquot;)) dbconn.Open() sql=quot;SELECT * FROM customersquot; dbcomm=New OleDbCommand(sql,dbconn) dbread=dbcomm.ExecuteReader() customers.DataSource=dbread customers.DataBind() dbread.Close() dbconn.Close() end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:Repeater id=quot;customersquot; runat=quot;serverquot;> <HeaderTemplate> <table border=quot;1quot; width=quot;100%quot;> <tr> <th>Companyname</th> <th>Contactname</th> <th>Address</th> <th>City</th>