Database connectivity with data reader by varun tiwari

949 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
949
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Database connectivity with data reader by varun tiwari

  1. 1. 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
  2. 2. 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)
  3. 3. 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

×