DataReader:
->Datareader is readonly and forwardonly recordset
->readonly means datareader doesn’t support modifying record, it supports only reading data
->forwardonly means datareader supports recordpointer navigation in forward direction if doesn’t allow reading records more than one time
->datareader class doesn’t allow object creation, the object will be returned by executereader method
Ex:cmd->select * from product
Oledbdatareader dr;
dr=cmd.executereader()
the main advantage of datareader is consuming only 1 record memory at a time with in application process
Methods:
1)read()
It will fetch a record into application process, It returns true if record is fetched else returns false
2)get string(colindex)
This method can be used to read column data of type string
3)get int(colindex)
This method can be used to read int type column data
Ex:dr.getstring(0)->returns poo1
dr.get<datatype>(colindex)
4)dr[colindex/colname]->indexer
This can be used to read column data of any type, it returns value in the form of an object
Ex:dr[“prodid”]
5)field count:
It returns table columns count
6)close()
This can be used to close datareader, it will release RAM memory
Note:
1)datareader will function if connection is opened closing connection will close datareader
2)if the application requirement is reading data only once then go with datareader to consume less memory resources within application.
Working with access
->access comes with msoffice installation
->access 2003 database file extension is mdb
->access 2007 database file extension is accdb
Creating database with in access
Start->programs->MS office->access
->goto start menu (or) file menu of acces and select new option
Blank database
Bank.accdb[e:\aspnet131]
->rightclick on tabel1 and select design view, provide table name as customer
Colname datatype
Accno number
Accname text
Accbal number
Close new table window
Rightclick on customer table and select open then insert records
1 charan 20000
2 raju 15000
Close access database
steps to work with .net data provider
1.creating connection object
2.creating command object with required sql statement
3.calling required method with command object based on sql statement
4.if it is executedreader then datareader is required
5.datareader can be used with if start if select is providing only 1 record (or) datareader can be used with while if select is providing more than one record
6.closing connection
Creating website to connect to acces using oledb.net
Goto visual studio
Start->run->devenv
It will display main window of visual studio
File menu->new->website->visual c#->select asp.net empty website
Weblocation->e:\aspnet\obcsite[drive:\dir\websitename]
Visual studio create a folder with website name, in this folder website related files will be placed
Add webform
Goto view menu and select solution explorer
Right click on website path and select add new item
Select webform
Give name as display.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
goto design part
accno
name
balance
display |
coding: goto view and select codeoption
using System.Data.OleDb;(name space)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public partial class display : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("provider=microsoft.ace.OleDb.12.0;data source=e:\\aspnet131\\bank.accdb");
OleDbCommand cmd = new OleDbCommand("select * from customer where accno=" + TextBox1.Text,con);
OleDbDataReader dr;
con.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox2.Text = dr["accname"].ToString();
TextBox3.Text = dr["accbal"].ToString();
}
else
Response.Write("<h2>accno not found..</h2>");
dr.Close();
con.Close();
}
}
Goto contrl F5