Database connectivity with data reader by varun tiwari

3
Database Connectivity with Data Reader Figure 17.5 Step 1.Create a database Employee in Ms Access 2003/2007/2010/2013 and then create a table Emp. Field of 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 connecons then select add connecon. Now select data source Microsoſt Access Database file and then select the database file name. Where the database is stored and then click ok buon. Step 5. When the connecon is created then copy the path of file in properes window. Path is this: "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Users\Tiwari\Desktop\Employee.mdb") Then design a form, coding in a form and then run it. Example 15.2 Imports System.Data.OleDb Public 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 As System.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 As System.EventArgs) Handles Button1.Click 'Save Record Try

Transcript of Database connectivity with data reader by varun tiwari

Page 1: Database connectivity with data reader by varun tiwari

Database Connectivity with Data Reader

Figure 17.5

Step 1.Create a database Employee in Ms Access 2003/2007/2010/2013 and then create a table Emp. Field

of 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 Microsoft Access Database file and then select the database file name. Where the database is stored and then click ok 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:\Users\Tiwari\Desktop\Employee.mdb")

Then design a form, coding in a form and then run it.

Example 15.2 Imports System.Data.OleDb

Public 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 As

System.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 As

System.EventArgs) Handles Button1.Click

'Save Record

Try

Page 2: Database connectivity with data reader by varun tiwari

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=C:\Users\Tiwari\Desktop\Employee.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 As

System.EventArgs) Handles Button3.Click

'Update Record

Try

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=C:\Users\Tiwari\Desktop\Employee.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 As

System.EventArgs) Handles Button2.Click

'Delete Record

Try

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=C:\Users\Tiwari\Desktop\Employee.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 As

System.EventArgs) Handles Button5.Click

'Show Record

Try

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=C:\Users\Tiwari\Desktop\Employee.mdb")

con.Open()

cmd = New OleDbCommand("select * from Emp", con)

Page 3: Database connectivity with data reader by varun tiwari

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 As

System.EventArgs) Handles MyBase.Load

Try

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=C:\Users\Tiwari\Desktop\Employee.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 As

System.EventArgs) Handles Button6.Click

Application.Exit()

End Sub

End Class