|
ADO.NET, the data access technology built into the .NET framework.
Data Providers:
System.Data.SqlClient : contains classes that can communicate with Ms SQl Server.
System.Data.OracleClient : responsible for communicating with Oracle.
System.Data.Oledb : contains classes for communicating to a data source that has an OLEDB provider
System.Data.Odbc : contains classes for communicating to a data source that has an ODBC driver.
SQLConnection, SQLCommand, SQLDataReader:
SqlConnection: This class is responsible to create and open connection with sql server.
SqlCommand: The class represent a sql statement or stored procedure.
SqlDataReader: This class represent the results from a database Query.
Retrieving Records from a Database table:
- Create and open a database connection.
- Create a database command and execute it.
- Execute the command with the executeReader () and return datareader.
- Iterate through the reader object.
Execute Reader: Fetching Records from Database
SQLDataReader.aspx
<% @ Page Language ="VB" AutoEventWireup ="false" CodeFile ="Example5.aspx.vb" Inherits ="Example5" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml" >
<head runat ="server">
</head>
<body>
<form id ="form1" runat ="server">
<div>
<asp:Label ID ="Label1" runat ="server" Text ="Display Employee Name"></asp:Label>
<asp:DropDownList ID ="DropDownList1" runat ="server">
</asp:DropDownList></div>
</form>
</body>
</html>
SQLDataReader.aspx.vb
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Example5 Inherits System.Web.UI.Page
Protected Sub Page_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dtr As SqlDataReader
con = New SqlConnection( "Data Source=localhost;Initial catalog=TestDB;UID=sa;pwd=sa" )
cmd = New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from tblEmp"
con.Open()
dtr = cmd.ExecuteReader
DropDownList1.DataSource = dtr
DropDownList1.DataTextField = "empName"
DropDownList1.DataValueField = "empId"
DropDownList1.DataBind()
con.Close()
End Sub
End Class
|