You are on page 1of 10

1

Company Database ERD

Connected Environment or Active Connection

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


Dim con As SqlConnection
Dim comm As SqlCommand
Dim rd As SqlDataReader

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "data source=Majeed;initial catalog=company;user
id=sa;Password=11326"
comm = New SqlCommand
comm.Connection = con
'For Fill Deptartment Combo
comm = New SqlCommand("Select * from Dept")
comm.CommandType = CommandType.Text
comm.Connection = con
If comm.Connection.State = ConnectionState.Closed Then
comm.Connection.Open()
End If
rd = comm.ExecuteReader
cbodeptname.Items.Clear()
While rd.Read
cbodeptname.Items.Add(rd("deptno") & "-" & rd("DeptName"))
End While
rd.Close()
comm.Dispose()
End Sub
2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
EmptyForm() ‘Procedure Look at Db01
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
Dim qry As String
Dim cnt As Integer
qry = "insert into emp values (" & _
TextBox1.Text & "," & _
"'" & TextBox2.Text & "'," & _
TextBox3.Text & "," & _
TextBox4.Text & ")"
comm.CommandText = qry
comm.Connection.Open()
cnt = comm.ExecuteNonQuery()
comm.Connection.Close()
MessageBox.Show(cnt & " record affected")
EmptyForm()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button3.Click
Dim qry As String
qry = "select * from emp" & _
" where empno=" & TextBox1.Text
comm.CommandText = qry
comm.Connection.Open()
rd = comm.ExecuteReader
'Initially the record pointer is at top within the reader object
If rd.HasRows Then
rd.Read() 'Move to the next record
TextBox2.Text = rd("ename")
TextBox3.Text = rd("sal")
TextBox4.Text = rd("deptno")
Else
MessageBox.Show("Record not found")
EmptyForm()
End If
comm.Connection.Close()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button4.Click
Dim qry As String
Dim cnt As Integer
qry = "update emp set " & _
"ename='" & TextBox2.Text & "'," & _
"sal=" & TextBox3.Text & "," & _
"deptno=" & TextBox4.Text & _
" where empno=" & TextBox1.Text
comm.CommandText = qry
comm.Connection.Open()
cnt = comm.ExecuteNonQuery()
comm.Connection.Close()
MessageBox.Show(cnt & " record affected")
EmptyForm()
End Sub
3
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Dim qry As String
Dim cnt As Integer
qry = "delete from emp" & _
" where empno=" & TextBox1.Text
comm.CommandText = qry
comm.Connection.Open()
cnt = comm.ExecuteNonQuery()
comm.Connection.Close()
MessageBox.Show(cnt & " record affected")
EmptyForm()
End Sub
End Class

Disconnected Environment or Deactive Connection


Db01

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


Dim con As SqlConnection
Dim adapt As SqlDataAdapter
Dim adaptBld As SqlCommandBuilder
Dim ds As DataSet
Dim drFound As DataRow = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
'1
con = New SqlConnection
con.ConnectionString = "data source=Majeed;initial catalog=company;user
id=sa;password=11326"
'2
adapt = New SqlDataAdapter("select * from emp", con)
'3
adaptBld = New SqlCommandBuilder(adapt)
'4
ds = New DataSet()
'5
adapt.Fill(ds, "empTable")
End Sub
4
Private Sub EmptyForm()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox1.Focus()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
EmptyForm()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
Dim dr As DataRow
dr = ds.Tables("empTable").NewRow()
dr("empno") = Convert.ToInt32(TextBox1.Text)
dr("ename") = TextBox2.Text
dr("sal") = Convert.ToSingle(TextBox3.Text)
dr("deptno") = Convert.ToInt32(TextBox4.Text)
ds.Tables("empTable").Rows.Add(dr)
MessageBox.Show("Record Inserted in Memory")
EmptyForm()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button3.Click
Dim eno As Integer = Convert.ToInt32(TextBox1.Text)
Dim x As Integer
For x = 0 To ds.Tables("empTable").Rows.Count - 1
If Convert.ToInt32(ds.Tables("empTable").Rows(x)("empno")) = eno Then
drFound = ds.Tables("empTable").Rows(x)
Exit For
End If
Next
If drFound IsNot Nothing Then
TextBox2.Text = drFound("ename").ToString()
TextBox3.Text = drFound("sal").ToString()
TextBox4.Text = drFound("deptno").ToString()
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button4.Click
If drFound IsNot Nothing Then
drFound("ename") = TextBox2.Text
drFound("sal") = Convert.ToSingle(TextBox3.Text)
drFound("deptno") = Convert.ToInt32(TextBox4.Text)
MessageBox.Show("Record Updated in Memory")
End If
EmptyForm()
drFound = Nothing
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button5.Click
If drFound IsNot Nothing Then
drFound.Delete()
MessageBox.Show("Record Deleted from memory")
End If
EmptyForm()
drFound = Nothing
End Sub
5
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click
adapt.Update(ds, "empTable")
MessageBox.Show("Changes Saved in Database")
End Sub
End Class

Db02 ‘Form Layout same as Db01


Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim con As SqlConnection
Dim adapt As SqlDataAdapter
Dim adaptBld As SqlCommandBuilder
Dim deptAdapt As SqlDataAdapter
Dim ds As DataSet
Dim drFound As DataRow = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "data source=Majeed;initial catalog=company;user
id=sa;password=11326"
adapt = New SqlDataAdapter("select * from emp", con)
adaptBld = New SqlCommandBuilder(adapt)
deptAdapt = New SqlDataAdapter("select * from dept", con)
ds = New DataSet()
adapt.Fill(ds, "empTable")
deptAdapt.Fill(ds, "deptTable")
'Adding constraints
ds.Tables("deptTable").Constraints.Add("deptPk", New DataColumn()
{ds.Tables("deptTable").Columns("deptno")}, True)
ds.Tables("empTable").Constraints.Add("empPk", New DataColumn()
{ds.Tables("empTable").Columns("empno")}, True)
ds.Tables("empTable").Constraints.Add("empFk", New DataColumn()
{ds.Tables("deptTable").Columns("deptno")}, New DataColumn()
{ds.Tables("empTable").Columns("deptno")})
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
EmptyForm() ‘Procedure Look at Db01
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
Dim dr As DataRow
dr = ds.Tables("empTable").NewRow()
dr("empno") = Convert.ToInt32(TextBox1.Text)
dr("ename") = TextBox2.Text
dr("sal") = Convert.ToSingle(TextBox3.Text)
dr("dno") = Convert.ToInt32(TextBox4.Text)
Try
ds.Tables("empTable").Rows.Add(dr)
MessageBox.Show("Record Inserted in Memory")
EmptyForm()
Catch exp As ConstraintException
MessageBox.Show("PK: " & exp.Message)
Catch exp As InvalidConstraintException
MessageBox.Show("FK: " & exp.Message)
End Try
End Sub
6
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim eno As Integer = Convert.ToInt32(TextBox1.Text)
drFound = ds.Tables("empTable").Rows.Find(New Object() {eno})
If drFound IsNot Nothing Then
TextBox2.Text = drFound("ename").ToString()
TextBox3.Text = drFound("sal").ToString()
TextBox4.Text = drFound("deptno").ToString()
Else
MessageBox.Show("Record not Found")
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button4.Click
If drFound IsNot Nothing Then
Try
drFound("ename") = TextBox2.Text
drFound("sal") = Convert.ToSingle(TextBox3.Text)
drFound("deptno") = Convert.ToInt32(TextBox4.Text)
MessageBox.Show("Record Updated in Memory")
EmptyForm()
Catch exp As InvalidConstraintException
MessageBox.Show("FK: " & exp.Message)
End Try
End If
drFound = Nothing
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button5.Click
If drFound IsNot Nothing Then
drFound.Delete()
MessageBox.Show("Record Deleted from memory")
End If
EmptyForm()
drFound = Nothing
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button6.Click
adapt.Update(ds, "empTable")
MessageBox.Show("Changes Saved in Database")
End Sub
End Class
7

Db03 ‘Form Layout same as Db01

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim con As SqlConnection
Dim adapt As SqlDataAdapter
Dim adaptBld As SqlCommandBuilder
Dim deptAdapt As SqlDataAdapter
Dim ds As CompanyDataSet
Dim drFound As CompanyDataSet.empRow = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "data source=Majeed;initial catalog=company;user
id=sa;password=11326"
adapt = New SqlDataAdapter("select * from emp", con)
adaptBld = New SqlCommandBuilder(adapt)
deptAdapt = New SqlDataAdapter("select * from dept", con)
ds = New CompanyDataSet
deptAdapt.Fill(ds, "dept")
adapt.Fill(ds, "emp")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
EmptyForm() ‘Procedure Look at Db01
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
Dim dr As CompanyDataSet.empRow
dr = ds.emp.NewempRow
dr.empno = Convert.ToInt32(TextBox1.Text)
dr.ename = TextBox2.Text
dr.sal = Convert.ToSingle(TextBox3.Text)
dr.deptno = Convert.ToInt32(TextBox4.Text)
Try
ds.emp.Rows.Add(dr)
MessageBox.Show("Record Inserted in Memory")
EmptyForm()
Catch exp As ConstraintException
MessageBox.Show("PK: " & exp.Message)
Catch exp As InvalidConstraintException
MessageBox.Show("FK: " & exp.Message)
End Try
8
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button3.Click
Dim eno As Integer = Convert.ToInt32(TextBox1.Text)
drFound = ds.emp.FindByempno(eno)
If drFound IsNot Nothing Then
TextBox2.Text = drFound.ename
TextBox3.Text = drFound.sal.ToString()
TextBox4.Text = drFound.deptno.ToString()
Else
MessageBox.Show("Record not Found")
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button4.Click
If drFound IsNot Nothing Then
Try
drFound.ename = TextBox2.Text
drFound.sal = Convert.ToSingle(TextBox3.Text)
drFound.deptno = Convert.ToInt32(TextBox4.Text)
MessageBox.Show("Record Updated in Memory")
EmptyForm()
Catch exp As InvalidConstraintException
MessageBox.Show("FK: " & exp.Message)
End Try
End If
drFound = Nothing
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button5.Click
If drFound IsNot Nothing Then
drFound.Delete()
MessageBox.Show("Record Deleted from memory")
End If
EmptyForm()
drFound = Nothing
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button6.Click
adapt.Update(ds, "emp")
MessageBox.Show("Changes Saved in Database")
End Sub
End Class
Db04
9

Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Dim con As SqlConnection
Dim empAdapt As SqlDataAdapter
Dim bldAdapt As SqlCommandBuilder
Dim empSource As BindingSource
Dim companyDs As DataSet1
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "data source=Majeed;initial catalog=company;user
id=sa;password=11326"
empAdapt = New SqlDataAdapter("select * from emp", con)
bldAdapt = New SqlCommandBuilder(empAdapt)
companyDs = New DataSet1()
empAdapt.Fill(companyDs, "emp")
'A
empSource = New BindingSource()
empSource.DataSource = companyDs
empSource.DataMember = "emp"
'B
empNavigator.BindingSource = empSource
'C
TextBox1.DataBindings.Add(New Binding("Text", empSource, "empno"))
TextBox2.DataBindings.Add(New Binding("Text", empSource, "ename"))
TextBox3.DataBindings.Add(New Binding("Text", empSource, "sal"))
TextBox4.DataBindings.Add(New Binding("Text", empSource, "deptno"))
End Sub

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles SaveToolStripButton.Click
Try
empAdapt.Update(companyDs, "emp")
Catch exp As Exception
MessageBox.Show(exp.Message)
End Try
MessageBox.Show("Changes Saved")
End Sub
End Class

Connection String With Microsoft Access

Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
10
Dim comm As OleDbCommand
Dim rd As OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
con = New OleDbConnection
con.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & _
Application.StartupPath & "\company.mdb"
End Sub
End Class

You might also like