Create an application in vb which insert, delete and update data in the database.
Program:
Design:
Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim str As String
Private Sub btninsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btninsert.Click
conn = New SqlConnection("Data Source=SHIMEKA-PC;Initial Catalog=sizar;Persist Security Info=True;User ID=sa;Password=123")
conn.Open()
If rbtnmale.Checked Then
Str = rbtnmale.Text
Else
str = rbtnfemale.Text
End If
cmd = New SqlCommand("Insert into stu(id,name,surname,number,gender,city) values('" & txtid.Text & "','" & txtname.Text & "','" & txtsurname.Text & "','" & txtnumber.Text & "','" & str & "','" & cbcity.SelectedItem & "')", conn)
cmd.ExecuteNonQuery()
conn.Close()
reset()
MessageBox.Show("value inserted successfully")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn = New SqlConnection("Data Source=SHIMEKA-PC;Initial Catalog=sizar;Persist Security Info=True;User ID=sa;Password=123")
conn.Open()
cmd = New SqlCommand("DELETE from stu where id='" & txtid.Text & "'", conn)
cmd.ExecuteNonQuery()
conn.Close()
reset()
MessageBox.Show(" row deleted successfully")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
conn = New SqlConnection("Data Source=SHIMEKA-PC;Initial Catalog=sizar;Persist Security Info=True;User ID=sa;Password=123")
conn.Open()
If rbtnmale.Checked Then
str = rbtnmale.Text
Else
str = rbtnfemale.Text
End If
cmd = New SqlCommand("update stu set surname='" & txtsurname.Text & "',number='" & txtnumber.Text & "',name='" & txtname.Text & "',gender='" & str & "',city='" & cbcity.SelectedItem & "' where id='" & txtid.Text & "'", conn)
cmd.ExecuteNonQuery()
conn.Close()
reset()
MessageBox.Show(" row updated successfully")
End Sub
Public Sub reset()
txtid.Text = ""
txtname.Text = ""
txtnumber.Text = ""
txtsurname.Text = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Program:
Design:
Create an application in vb which insert, delete and update data in the database |
Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim str As String
Private Sub btninsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btninsert.Click
conn = New SqlConnection("Data Source=SHIMEKA-PC;Initial Catalog=sizar;Persist Security Info=True;User ID=sa;Password=123")
conn.Open()
If rbtnmale.Checked Then
Str = rbtnmale.Text
Else
str = rbtnfemale.Text
End If
cmd = New SqlCommand("Insert into stu(id,name,surname,number,gender,city) values('" & txtid.Text & "','" & txtname.Text & "','" & txtsurname.Text & "','" & txtnumber.Text & "','" & str & "','" & cbcity.SelectedItem & "')", conn)
cmd.ExecuteNonQuery()
conn.Close()
reset()
MessageBox.Show("value inserted successfully")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn = New SqlConnection("Data Source=SHIMEKA-PC;Initial Catalog=sizar;Persist Security Info=True;User ID=sa;Password=123")
conn.Open()
cmd = New SqlCommand("DELETE from stu where id='" & txtid.Text & "'", conn)
cmd.ExecuteNonQuery()
conn.Close()
reset()
MessageBox.Show(" row deleted successfully")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
conn = New SqlConnection("Data Source=SHIMEKA-PC;Initial Catalog=sizar;Persist Security Info=True;User ID=sa;Password=123")
conn.Open()
If rbtnmale.Checked Then
str = rbtnmale.Text
Else
str = rbtnfemale.Text
End If
cmd = New SqlCommand("update stu set surname='" & txtsurname.Text & "',number='" & txtnumber.Text & "',name='" & txtname.Text & "',gender='" & str & "',city='" & cbcity.SelectedItem & "' where id='" & txtid.Text & "'", conn)
cmd.ExecuteNonQuery()
conn.Close()
reset()
MessageBox.Show(" row updated successfully")
End Sub
Public Sub reset()
txtid.Text = ""
txtname.Text = ""
txtnumber.Text = ""
txtsurname.Text = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
No comments:
Post a Comment