Database management insert,delete,update,search in microsoft access February 25, 2008
Posted by Shamim Ahmed in VB.NET.trackback
Today I have run a database management program using SQL,Oracal and microsoft access database system here i given the procedure how insert data in microsoft access if you follow my instruction then you will be successed
General Declaration:
Imports System
Imports System.Data
Imports System.Data.OleDb
Write the following code on the click event:
Dim con As OleDbConnection
Dim str As String
Dim sql As String
str = “Provider=Microsoft.Jet.oledb.4.0;Data Source=C:shamim.mdb;”
con = New OleDbConnection
(str)sql = “insert into students(name,roll,email,mobile)values(‘” & TextBox1.Text & “‘,’” & TextBox2.Text & “‘,’” & TextBox3.Text & “‘,’” & TextBox4.Text & “‘)”
Dim cmd As OleDbCommand
Try
con.Open()cmd = New OleDbCommand
(Sql, con)cmd.ExecuteNonQuery()
TextBox1.Text = “”
TextBox2.Text = “”
TextBox3.Text = “”
TextBox4.Text = “” MessageBox.Show(“Insert Successfully”)con.Close()Catch ex As OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical, “Oledb Error”)
End Try
Now I will give you for update click: Dim con As OleDbConnectionDim str As String Dim sql As String
str = “Provider=Microsoft.Jet.oledb.4.0;
Data Source=C:shamim.mdb;”
con = New OleDbConnection
(str)sql = “delete from students where roll=’” & TextBox2.Text & “‘”
Dim cmd As OleDbCommand
Trycon.Open()cmd = New OleDbCommand(sql, con)cmd.ExecuteNonQuery()
TextBox1.Text = “”
TextBox2.Text = “”
TextBox3.Text = “”
TextBox4.Text = “” MessageBox.Show(“Deleted successfully”) con.Close() Catch ex As OleDbExceptionMsgBox(ex.Message, MsgBoxStyle.Critical, “Oledb Error”)End TryFor Updating data by Roll:
Dim con As OleDbConnection
Dim str As String
Dim sql As String str = “Provider=Microsoft.Jet.oledb.4.0;Data Source=C:shamim.mdb;”
con = New OleDbConnection(
str)sql = “update students set name=’” & TextBox1.Text & “‘,mobile=’” & TextBox4.Text & “‘,email=’” & TextBox3.Text & “‘ where roll=’” & TextBox2.Text & “‘” Dim cmd As OleDbCommand
Try
con.Open()cmd = New OleDbCommand
(sql, con)cmd.ExecuteNonQuery() TextBox1.Text = “” TextBox2.Text = “”TextBox3.Text = “” TextBox4.Text = “”
MessageBox.Show(“Updated successfully”)con.Close() Catch ex As OleDbException MsgBox(ex.Message, MsgBoxStyle.Critical, “Oledb Error”)End TryFor Searching by Roll: Dim con As OleDbConnection Dim sql As String Dim str As Stringstr = “Provider=Microsoft.Jet.oledb.4.0;Data Source=C:\shamim.mdb;”con = New OleDbConnection(Str)sql = “select * from students where roll=’” & TextBox2.Text & “‘”
Dim cmd As OleDbCommand
Dim r As OleDbDataReader
Try con.Open()cmd = New OleDbCommand(sql, con)r = cmd.ExecuteReader()
If r.Read
Then
TextBox1.Text = r(“name”)TextBox4.Text = r(“mobile”)TextBox3.Text = r( “email”) ElseTextBox1.Text = “” TextBox2.Text = “”
TextBox4.Text = “”
TextBox3.Text = “”
MessageBox.Show(“No Information found”)
End If con.Close() Catch ex As OleDbException MsgBox(ex.Message, MsgBoxStyle.Critical, “Oledb Error”)
Catch
ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, “General Error”) End Tryshawing all data from data table:
str as stringstr1 As String
Dim con As OleDbConnectionstr = “Provider=Microsoft.Jet.oledb.4.0;Data Source=C:\shamim.mdb;”
con = New OleDbConnection
(str) Dim da As OleDbDataAdapter Dim tb As DataTableTry con.Open()str1 = “select name,roll,mobile,email from students” con.Close()da = New OleDbDataAdapter
(str1, con)tb = New DataTable(“student”) da.Fill(tb) DataGridView1.DataSource = tbCatch ex As OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical, “Oledb Error”)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, “General Error”)
End Try
Due to unsufficient time i just pasted it from my program if you get some mising please inform me.
Nice ! Try to be a man who will be one’s favourite…
Thanks nuhil ! for your nice comment……….
good job
thats great , i copy these for self study