• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Database connectivity with data reader by varun tiwari
 

Database connectivity with data reader by varun tiwari

on

  • 1,040 views

 

Statistics

Views

Total Views
1,040
Views on SlideShare
1,040
Embed Views
0

Actions

Likes
1
Downloads
12
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Database connectivity with data reader by varun tiwari Database connectivity with data reader by varun tiwari Document Transcript

    • Database Connectivity with Data Reader Figure 17.5Step 1.Create a database Employee in Ms Access 2003/2007/2010/2013 and then create a table Emp. Fieldof EMP table is: Ecode, Ename, Eage, Edesig, and Esalary. Now adding the data into the table.Step 2. Open Visual Studio .Net 2008/2010/2012.Step 3. Click on view menu and open server explorer.Step 4. Right click on data connections then select add connection. Now select data source MicrosoftAccess Database file and then select the database file name. Where the database is stored and then clickok button.Step 5. When the connection is created then copy the path of file in properties window.Path is this:"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:UsersTiwariDesktopEmployee.mdb")Then design a form, coding in a form and then run it.Example 15.2Imports System.Data.OleDbPublic Class Form2 Dim con As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader Private Sub Button4_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button4.Click TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() TextBox5.Clear() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click Save Record Try
    • con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:UsersTiwariDesktopEmployee.mdb") con.Open() cmd = New OleDbCommand("insert into Emp values(" & TextBox1.Text& "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text &"," & TextBox5.Text & ") ", con) cmd.ExecuteNonQuery() MessageBox.Show("Record Inserted", "Updated",MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button3.Click Update Record Try con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:UsersTiwariDesktopEmployee.mdb") con.Open() cmd = New OleDbCommand("Update Emp set Ecode=" & TextBox1.Text &",Ename=" & TextBox2.Text & ",Eage=" & TextBox3.Text & ",Edesig=" &TextBox4.Text & ", Esalary= " & TextBox5.Text & " where Ecode=" &TextBox1.Text & " ", con) cmd.ExecuteNonQuery() dr = cmd.ExecuteReader MessageBox.Show("Record Updated", "Updated",MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click Delete Record Try con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:UsersTiwariDesktopEmployee.mdb") con.Open() cmd = New OleDbCommand("delete from Emp where Ecode=" &TextBox1.Text & "", con) cmd.ExecuteNonQuery() dr = cmd.ExecuteReader MessageBox.Show("Record Deleted", "Deleted",MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button5.Click Show Record Try con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:UsersTiwariDesktopEmployee.mdb") con.Open() cmd = New OleDbCommand("select * from Emp", con)
    • dr = cmd.ExecuteReader While dr.Read TextBox1.Text = dr(0) TextBox2.Text = dr(1) TextBox3.Text = dr(2) TextBox4.Text = dr(3) TextBox5.Text = dr(4) End While Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load Try con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:UsersTiwariDesktopEmployee.mdb") con.Open() cmd = New OleDbCommand("select * from Emp", con) dr = cmd.ExecuteReader While dr.Read ListBox1.Items.Add(dr(1)) End While Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button6.Click Application.Exit() End SubEnd Class