C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

OledbDatareader


Posted Date: 30 Jul 2009    Resource Type: Code Snippets    Category: ADO.NET
Author: NeetuMember Level: Diamond    
Rating: 1 out of 5Points: 10



Description :


This program is used to get the data from the database using datareader.This program will help the beginners of ADO.net.


Imports System.Data.OleDb
Public Class DATAREADERONE


'OleDbConnection is a class used to create the connection with the backend. and con is the object of OleDbConnection.



Dim con As OleDbConnection


'OleDbDataReader is abstract class used to read the data from the backend and it require open connection.



Dim datareader As OleDbDataReader


'OleDbCommand is used to bind with the table of the backend.


Dim cmd As OleDbCommand

'after the reterive of data it will be stored in the datatable


Dim dt As New DataTable

'System.Text.StringBuilder is a class provide by .net to concatenate the string and create the line.



Dim str As New System.Text.StringBuilder

Private Sub DATAREADERONE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'rtnstr is user build function used to create the connectionstring.

con = New OleDbConnection(rtnstr)
cmd = New OleDbCommand()


'OleDbCommand requires 3 things.
' one is commandText i.e is name of table or query or storeprocedure of backend
'second is commandtype whether we r dealing with the tableDirect or storedProcedure or sql query.
'third is connection string


cmd.CommandText = "studentdetails"
cmd.CommandType = CommandType.TableDirect
cmd.Connection = con
con.Open()


'ExceuteReader is method that will execute the command and return datareader.



datareader = cmd.ExecuteReader


'ReaderToTable is method which take datareader as an argument and return the datatable.


DataGridView1.DataSource = ReaderToTable(datareader)
End Sub

Private Function ReaderToTable(ByVal reader As OleDbDataReader) As DataTable

Dim newTable As New DataTable()
Dim col As DataColumn
Dim row As DataRow
Dim i As Integer

'This for loop is used to collect the column name and insert it into the datatable with their datatype.
For i = 0 To reader.FieldCount - 1
col = New DataColumn()
col.ColumnName = UCase(reader.GetName(i))
col.DataType = reader.GetFieldType(i)
newTable.Columns.Add(col)
Next

'It will create the row . After that row will be added in the datatable
While reader.Read

row = newTable.NewRow()
'data is collected from the reader. column by column
For i = 0 To reader.FieldCount - 1
row(i) = reader.Item(i)
Next

newTable.Rows.Add(row)
End While

Return newTable
End Function
Function rtnstr() As String
str.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=")
str.Append(AppDomain.CurrentDomain.BaseDirectory)
str.Append("yogita.mdb")
str.Append(";Persist Security Info=False")
Return str.ToString
End Function
End Class



Still have doubts check the attachements.

Regards
Neetu

Attachments

  • dealing with datareader (30911-301227-Datareader.rar)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Dealing with datareader  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Current Date & time
    Previous Resource: Using DataReader
    Return to Discussion Resource Index
    Post New Resource
    Category: ADO.NET


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use