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

EventArgs) Handles Button1.Open() cnt = comm. 2 Private Sub Button1_Click(ByVal sender As System.Close() MessageBox.CommandText = qry comm.HasRows Then rd.Read() 'Move to the next record TextBox2.Connection.Click Dim qry As String Dim cnt As Integer qry = "update emp set " & _ "ename='" & TextBox2.Text comm.Text = rd("sal") TextBox4.Open() rd = comm.Text & ".Connection.Text & "'.Text & "'.Close() MessageBox. ByVal e As System." & _ "deptno=" & TextBox4.Click Dim qry As String qry = "select * from emp" & _ " where empno=" & TextBox1.Object.Text & ". ByVal e As System.Text & _ " where empno=" & TextBox1." & _ "sal=" & TextBox3. ByVal e As System. ByVal e As System.Object." & _ "'" & TextBox2.Connection." & _ TextBox3.ExecuteNonQuery() comm.CommandText = qry comm.Click Dim qry As String Dim cnt As Integer qry = "insert into emp values (" & _ TextBox1." & _ TextBox4.Open() cnt = comm.EventArgs) Handles Button3.EventArgs) Handles Button4.Click EmptyForm() ‘Procedure Look at Db01 End Sub Private Sub Button2_Click(ByVal sender As System.Connection.ExecuteReader 'Initially the record pointer is at top within the reader object If rd.Text & ".CommandText = qry comm.Text = rd("ename") TextBox3.Object.Text comm.Close() End Sub Private Sub Button4_Click(ByVal sender As System.Object.EventArgs) Handles Button2.Show(cnt & " record affected") EmptyForm() End Sub .Connection.Show(cnt & " record affected") EmptyForm() End Sub Private Sub Button3_Click(ByVal sender As System.Connection.Text = rd("deptno") Else MessageBox.ExecuteNonQuery() comm.Text & ")" comm.Show("Record not found") EmptyForm() End If comm.

EventArgs) Handles MyBase.Show(cnt & " record affected") EmptyForm() End Sub End Class Disconnected Environment or Deactive Connection Db01 Imports System.Text comm.Connection.password=11326" '2 adapt = New SqlDataAdapter("select * from emp".ConnectionString = "data source=Majeed.EventArgs) Handles Button5. con) '3 adaptBld = New SqlCommandBuilder(adapt) '4 ds = New DataSet() '5 adapt.Close() MessageBox.initial catalog=company.Load '1 con = New SqlConnection con.Click Dim qry As String Dim cnt As Integer qry = "delete from emp" & _ " where empno=" & TextBox1.Connection.ExecuteNonQuery() comm.Fill(ds. ByVal e As System.CommandText = qry comm.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.Data Imports System.Object.user id=sa.Object. ByVal e As System. 3 Private Sub Button5_Click(ByVal sender As System.Open() cnt = comm. "empTable") End Sub .

Tables("empTable").Text) Dim x As Integer For x = 0 To ds.EventArgs) Handles Button3.Text = drFound("ename").Text dr("sal") = Convert.Object. ByVal e As System.Rows. ByVal e As System.Delete() MessageBox.Text) dr("deptno") = Convert.Rows.Object.Text = "" TextBox3.Text = drFound("sal").EventArgs) Handles Button5.Text) dr("ename") = TextBox2.Show("Record Inserted in Memory") EmptyForm() End Sub Private Sub Button3_Click(ByVal sender As System. ByVal e As System.EventArgs) Handles Button2.EventArgs) Handles Button1.Object. ByVal e As System.Click Dim eno As Integer = Convert.ToSingle(TextBox3.Text) ds.ToInt32(TextBox4.Text drFound("sal") = Convert.ToInt32(TextBox4.ToString() TextBox3.Text = drFound("deptno").Show("Record Deleted from memory") End If EmptyForm() drFound = Nothing End Sub .Rows(x)("empno")) = eno Then drFound = ds.Tables("empTable"). ByVal e As System.Tables("empTable").Text = "" TextBox4.Count .Add(dr) MessageBox.Click Dim dr As DataRow dr = ds.ToInt32(TextBox1.Rows(x) Exit For End If Next If drFound IsNot Nothing Then TextBox2.Focus() End Sub Private Sub Button1_Click(ByVal sender As System.Click If drFound IsNot Nothing Then drFound("ename") = TextBox2.ToString() End If End Sub Private Sub Button4_Click(ByVal sender As System.Click If drFound IsNot Nothing Then drFound.EventArgs) Handles Button4.NewRow() dr("empno") = Convert. 4 Private Sub EmptyForm() TextBox1.ToInt32(TextBox1.ToInt32(ds.1 If Convert.Text) MessageBox.Tables("empTable").Object.Text = "" TextBox2.Text) drFound("deptno") = Convert.ToSingle(TextBox3.Tables("empTable").Click EmptyForm() End Sub Private Sub Button2_Click(ByVal sender As System.Show("Record Updated in Memory") End If EmptyForm() drFound = Nothing End Sub Private Sub Button5_Click(ByVal sender As System.Object.ToString() TextBox4.Text = "" TextBox1.

Object.Fill(ds.Show("PK: " & exp.Text) dr("ename") = TextBox2.Columns("empno")}.Constraints. New DataColumn() {ds.Text) dr("dno") = Convert. New DataColumn() {ds.Constraints.Update(ds. "deptTable") 'Adding constraints ds.Object.EventArgs) Handles Button6.Add("empPk".user id=sa. "empTable") deptAdapt.Object.Columns("deptno")}.Add("empFk".Click Dim dr As DataRow dr = ds.Show("Record Inserted in Memory") EmptyForm() Catch exp As ConstraintException MessageBox.Fill(ds.Show("Changes Saved in Database") End Sub End Class Db02 ‘Form Layout same as Db01 Imports System. con) adaptBld = New SqlCommandBuilder(adapt) deptAdapt = New SqlDataAdapter("select * from dept".Constraints.Click EmptyForm() ‘Procedure Look at Db01 End Sub Private Sub Button2_Click(ByVal sender As System. ByVal e As System.Show("FK: " & exp.Tables("empTable").Rows. True) ds. ByVal e As System.Tables("empTable").Tables("empTable").ToSingle(TextBox3.Data. New DataColumn() {ds.Click adapt.Tables("empTable").Message) End Try End Sub .Message) Catch exp As InvalidConstraintException MessageBox.EventArgs) Handles Button1.Tables("deptTable").initial catalog=company. "empTable") MessageBox.Load con = New SqlConnection con.Object.ToInt32(TextBox4.Add("deptPk".Add(dr) MessageBox. ByVal e As System.ToInt32(TextBox1. ByVal e As System.NewRow() dr("empno") = Convert.EventArgs) Handles MyBase.ConnectionString = "data source=Majeed.Tables("deptTable").Tables("deptTable"). New DataColumn() {ds.Text dr("sal") = Convert. True) ds.Data Imports System. 5 Private Sub Button6_Click(ByVal sender As System. con) ds = New DataSet() adapt.Tables("empTable").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.Tables("empTable").EventArgs) Handles Button2.Columns("deptno")}) End Sub Private Sub Button1_Click(ByVal sender As System.Columns("deptno")}.Text) Try ds.password=11326" adapt = New SqlDataAdapter("select * from emp".

Show("Record Deleted from memory") End If EmptyForm() drFound = Nothing End Sub Private Sub Button6_Click(ByVal sender As System.Object. "empTable") MessageBox.EventArgs) Handles Button3.Click adapt.EventArgs) Handles Button6.Delete() MessageBox.Show("Record not Found") End If End Sub Private Sub Button4_Click(ByVal sender As System.ToSingle(TextBox3.Click If drFound IsNot Nothing Then Try drFound("ename") = TextBox2.Object.Text) drFound("deptno") = Convert.Object.Rows.Click If drFound IsNot Nothing Then drFound.Tables("empTable").ToInt32(TextBox4.Click Dim eno As Integer = Convert.ToString() TextBox4.ToString() Else MessageBox. 6 Private Sub Button3_Click(ByVal sender As System.Text = drFound("ename").EventArgs) Handles Button5.Text) MessageBox.Text = drFound("sal").Find(New Object() {eno}) If drFound IsNot Nothing Then TextBox2. ByVal e As System.ToString() TextBox3.Text) drFound = ds.Show("Changes Saved in Database") End Sub End Class .Update(ds. ByVal e As System.Show("Record Updated in Memory") EmptyForm() Catch exp As InvalidConstraintException MessageBox.Text drFound("sal") = Convert.ToInt32(TextBox1.Text = drFound("deptno").Show("FK: " & exp.EventArgs) Handles Button4. ByVal e As System.Object.Message) End Try End If drFound = Nothing End Sub Private Sub Button5_Click(ByVal sender As System. ByVal e As System.

empRow = Nothing Private Sub Form1_Load(ByVal sender As System. 7 Db03 ‘Form Layout same as Db01 Imports System.EventArgs) Handles MyBase.Message) End Try .Data Imports System.Show("Record Inserted in Memory") EmptyForm() Catch exp As ConstraintException MessageBox. con) adaptBld = New SqlCommandBuilder(adapt) deptAdapt = New SqlDataAdapter("select * from dept".Text) dr.empRow dr = ds.EventArgs) Handles Button2.Click EmptyForm() ‘Procedure Look at Db01 End Sub Private Sub Button2_Click(ByVal sender As System.password=11326" adapt = New SqlDataAdapter("select * from emp". ByVal e As System.Fill(ds.Fill(ds.Show("PK: " & exp.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. "emp") End Sub Private Sub Button1_Click(ByVal sender As System.Text dr.Add(dr) MessageBox. ByVal e As System.Text) Try ds. con) ds = New CompanyDataSet deptAdapt.Click Dim dr As CompanyDataSet.Object.user id=sa.ConnectionString = "data source=Majeed.ToInt32(TextBox1.sal = Convert.initial catalog=company.Message) Catch exp As InvalidConstraintException MessageBox.EventArgs) Handles Button1.Object.Data.emp.Text) dr.ename = TextBox2.Show("FK: " & exp.empno = Convert. "dept") adapt.ToSingle(TextBox3.NewempRow dr.Load con = New SqlConnection con.deptno = Convert.Rows.ToInt32(TextBox4. ByVal e As System.emp.Object.

Show("Record not Found") End If End Sub Private Sub Button4_Click(ByVal sender As System.Text) drFound = ds.ename TextBox3.Text) drFound.deptno = Convert.ToSingle(TextBox3.Text drFound.Text = drFound.ToString() TextBox4.EventArgs) Handles Button3.sal. ByVal e As System.Update(ds.Object.Click If drFound IsNot Nothing Then drFound.ToInt32(TextBox1.Text = drFound. ByVal e As System.emp.Show("Record Updated in Memory") EmptyForm() Catch exp As InvalidConstraintException MessageBox.ToInt32(TextBox4.Object.ename = TextBox2.EventArgs) Handles Button4.Delete() MessageBox.Click If drFound IsNot Nothing Then Try drFound. 8 End Sub Private Sub Button3_Click(ByVal sender As System.Message) End Try End If drFound = Nothing End Sub Private Sub Button5_Click(ByVal sender As System.Text = drFound.Click Dim eno As Integer = Convert. ByVal e As System.Text) MessageBox.Show("Record Deleted from memory") End If EmptyForm() drFound = Nothing End Sub Private Sub Button6_Click(ByVal sender As System.Show("FK: " & exp. ByVal e As System. "emp") MessageBox.sal = Convert.Show("Changes Saved in Database") End Sub End Class Db04 .Object.EventArgs) Handles Button5.Object.Click adapt.FindByempno(eno) If drFound IsNot Nothing Then TextBox2.EventArgs) Handles Button6.deptno.ToString() Else MessageBox.

empSource. 9 Imports System.Add(New Binding("Text".DataSource = companyDs empSource.Data Imports System.DataBindings.Object.Message) End Try MessageBox.Add(New Binding("Text". con) bldAdapt = New SqlCommandBuilder(empAdapt) companyDs = New DataSet1() empAdapt.Data. empSource. ByVal e As System. empSource.DataMember = "emp" 'B empNavigator.Update(companyDs.Add(New Binding("Text".Show("Changes Saved") End Sub End Class Connection String With Microsoft Access Imports System.Add(New Binding("Text".user id=sa.password=11326" empAdapt = New SqlDataAdapter("select * from emp".EventArgs) Handles MyBase.Data.DataBindings.DataBindings. "emp") 'A empSource = New BindingSource() empSource.Show(exp.Click Try empAdapt.DataBindings.initial catalog=company.Object.Fill(companyDs. "emp") Catch exp As Exception MessageBox. "sal")) TextBox4. ByVal e As System.Load con = New SqlConnection con. "empno")) TextBox2.BindingSource = empSource 'C TextBox1. empSource.OleDb Public Class Form1 Dim con As OleDbConnection .EventArgs) Handles SaveToolStripButton.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. "ename")) TextBox3. "deptno")) End Sub Private Sub SaveToolStripButton_Click(ByVal sender As System.ConnectionString = "data source=Majeed.

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