0% found this document useful (0 votes)
24 views13 pages

Practical No 27

GAD

Uploaded by

yadnyabothe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

Practical No 27

GAD

Uploaded by

yadnyabothe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Practical No.

27
Understand the concept of select and insert data in database table.

I. Practical Significance:
In VB.Net for connection & communication with database to data retrieval & updating
with data access controls.

II. Relevant Program Outcomes (POs)


Basic knowledge: Apply knowledge of basic mathematics, sciences and basic
engineering to solve the computer group related problems.

Discipline knowledge: Apply Computer Programming knowledge to solve the


computer group related problems.

Experiments and practice: Plan to perform experiments and practices to use the
results to solve the computer group related problems.

Engineering tools: Apply relevant Computer programming / technologies and tools


with an understanding of the limitations.

Communication: Communicate effectively in oral and written form.

III. Competency and Practical Skills


This practical expects to develop the following skills in the student.

Develop VB.NET programs to solve computer group related problems.

• Write an NET program for database connection with retrieve and store.
• Compile/Debug/Save the „VB.NET‟

IV. Relevant Course Outcomes


Use Data access controls to store data in Database and retrieve it.

V. Practical Outcome
Write a VB.Net Code to store and retrieve data in Database Table.

VI. Relevant Affective domain related Outcome(s)

• Follow safety
• Follow ethical

VII. Minimum Theoretical Background


A Command object executes SQL statements on the database. These SQL statements
can be SELECT, INSERT, UPDATE, or DELETE. It uses a connection object to perform
these actions on the database.

Syntax of SELECT, INSERT, UPDATE and DELETE SELECT


cmd =new SqlCommand(“select * from Employee”, con);

INSERT

cmd = new SqlCommand(“INSERT INTO Employee(Emp_ID, Emp_Name)VALUES (‘” +


aa + “‘,'” + bb + “‘)”, con);

UPDATE

SqlCommand cmd =new SqlCommand(“UPDATE Employee SET Emp_ID ='” + aa +

“‘, Emp_Name ='” + bb + “‘ WHERE Emp_ID = ‘” + aa + “‘”, con);

DELETE

cmd =new SqlCommand(“DELETE FROM Employee where Emp_ID='” + aa + “‘”, con);

A Command object exposes several execute methods like:

• ExecuteScaler()

Executes the query, and returns the first column of the first row in the result set
returned by the query. Extra columns or rows are ignored. dr =
cmd.ExecuteScaler();

– ExecuteReader()
Display all columns and all rows at client-side environment. In other words, we can
say that they display datatables client-side.

dr = cmd.ExecuteReader();

– ExecuteNonQuery()
Something is done by the database but nothing is returned by the database. dr =
cmd.ExecuteNonQuery();

VIII. Resources required (Additional)

• If any web resources required.

IX. Precautions

• Save the program in specific directory /


• Follow safety

X. Resources used (Additional)

• https://www.tutorialspoint.com/vb.net/vb.net

XI. Program Code: (Teacher must assign separate program statement


to group of 3-4 student)
Write a program to insert the data & retrieve the data from database.

1. Write a program to insert the data & retrieve the data from database.

Imports System.Data.OleDb

Public Class Form1

Dim con As OleDbConnection

Dim ds As New DataSet


Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Insert and
Retrieve\db1.mdb”)

con.Open()

Dim InsertString As String

InsertString = “Insert into student(RollNo, Name, Marks) values(‘” + txtRollNo.Text


+ “‘,'” + txtName.Text + “‘ ,'” + txtMarks.Text + “‘)”

Dim cmd As New OleDbCommand(InsertString, con)

cmd.ExecuteNonQuery()

MsgBox(” Record Successfully Inserted”)

con.Close()

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Insert and
Retrieve\db1.mdb”)

con.Open()

Dim cmd As New OleDbCommand(“Select * From student”, con)

Dim da As New OleDbDataAdapter(cmd)

DataGrid1.CaptionText = “Student Marks”

da.Fill(ds, “student”)

DataGrid1.DataSource = ds
DataGrid1.DataMember = “student”

con.Close()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Insert and
Retrieve\db1.mdb”)

con.Open()

Dim DeleteString As String

DeleteString = “Delete From student where RollNo ='” + txtRollNo.Text + “‘”

Dim cmd As New OleDbCommand(DeleteString, con)

cmd.ExecuteNonQuery()

MsgBox(” Record Successfully Deleted”)

con.Close()

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

Close()

End Sub

End Class

XII. Results (Output of the Program)


XIII. Practical Related Questions
Note: Below given are few sample questions for reference. Teacher must design
more such questions so as to ensure the achievement of identified CO.

• Write syntax of command object execute

(Space for answers)

1. Write syntax of command object execute method.

• ExecuteScaler():

dr = cmd.ExecuteScaler();

• ExecuteReader():

dr = cmd.ExecuteReader();

• ExecuteNonQuery():

dr = cmd.ExecuteNonQuery();

XIV. Exercise (Teacher must assign separate exercise to group of 3-4


student)

• Write working of each line in the following code

Dim strQuery As String = “select Name, ContentType, Data from tblFiles where
id=@id”
Dim cmd As SqlCommand = New SqlCommand(strQuery)

cmd.Parameters.Add(“@id”, SqlDbType.Int).Value = 1

Dim dt As DataTable = GetData(cmd)

If dt IsNot Nothing Then

download(dt)

End If

• Design a simple Windowsform for accepting the details of Using the connected
architecture of ADO.NET, perform the following operations:

• Insert
• Search
• Update
• Delete

(Space for answers)

1. Design a simple Windowsform for accepting the details of Employee.


Using the connected architecture of ADO.NET, perform the following
operations:

• Insert Record
• Search Record
• Update Record
• Delete Record
Imports System.Data.OleDb

Public Class Form1

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter

Private Sub btnIns_Click(sender As Object, e As EventArgs) Handles btnIns.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Employee\
Employee.mdb”)

con.Open()

If txtNo.Text = “” Then

MsgBox(“Please Enter Employee Number”)

Else

Dim InsertString As String


InsertString = “Insert into Employee(Emp_No, Emp_Name, Address,
Date_of_joining) values(‘” + txtNo.Text + “‘,'” + txtName.Text + “‘ ,'” + txtAdd.Text +
“‘, ‘” + txtDOJ.Text + “‘)”

Dim cmd As New OleDbCommand(InsertString, con)

cmd.ExecuteNonQuery()

MsgBox(” Record Successfully Inserted”)

txtNo.Clear()

txtName.Clear()

txtAdd.Clear()

txtDOJ.Clear()

con.Close()

End If

End Sub

Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Employee\
Employee.mdb”)

con.Open()

If txtNo.Text = “” Then

MsgBox(“Please Enter Employee Number”)

Else

Dim DeleteString As String

DeleteString = “Delete From Employee where Emp_No ='” + txtNo.Text + “‘”

Dim cmd As New OleDbCommand(DeleteString, con)

cmd.ExecuteNonQuery()
MsgBox(” Record Successfully Deleted”)

txtNo.Clear()

txtNo.Focus()

con.Close()

End If

End Sub

Private Sub btnUpd_Click(sender As Object, e As EventArgs) Handles btnUpd.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Employee\
Employee.mdb”)

con.Open()

If txtNo.Text <> “” Then

Dim UpdateString As String

UpdateString = “Update Employee SET Emp_No ='” + txtNo.Text +


“‘,Emp_Name='” + txtName.Text + “‘,Address='” + txtAdd.Text + “‘,Date_of_joining='” +
txtDOJ.Text + “‘ Where Emp_No ='” + txtNo.Text + “‘”

Dim cmd As New OleDbCommand(UpdateString, con)

cmd.ExecuteNonQuery()

MsgBox(” Record Successfully Updated”)

txtNo.Clear()

txtName.Clear()

txtAdd.Clear()

txtDOJ.Clear()

con.Close()

Else
MsgBox(” Please Enter Employment Number”)

End If

End Sub

Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles


btnShow.Click

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio
2012\Projects\Employee\Employee.mdb”)

con.Open()

Dim cmd As New OleDbCommand(“Select * From Employee”, con)

Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet

DataGrid1.CaptionText = “Employee Record”

da.Fill(ds, “Employee”)

DataGrid1.DataSource = ds

DataGrid1.DataMember = “Employee”

con.Close()

End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles


btnSearch.Click

If txtNo.Text = “” Then

MsgBox(“Please Enter Employee Number”)

Else

Dim con As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio
2012\Projects\Employee\Employee.mdb”)
con.Open()

Dim cmd As New OleDbCommand(“Select * From Employee where Emp_No like


‘” + txtNo.Text + “‘”, con)

Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet

DataGrid1.CaptionText = “Employee Record”

da.Fill(ds, “Employee”)

DataGrid1.DataSource = ds.Tables(“Employee”)

da.Dispose()

con.Close()

txtNo.Clear()

txtNo.Focus()

End If

End Sub

End Class

OUTPUT:

You might also like