Insert Data
Insert Data
SqlClient
Public Class Form2 Inherits
System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
'integer holds the number of records inserted
Private Sub Form2_Load(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;databas
e=pubs")
'you need to provide password for sql server
myConnection.Open()
myCommand = New SqlCommand("Insert into Jobs
values 12,'IT Manager',100,300,_
myConnection)
ra=myCommand.ExecuteNonQuery()
MessageBox.Show("New Row Inserted" & ra)
myConnection.Close()
End Sub
End Class
Deleting a Record
We will use Authors table in Pubs sample database to work with this
code. Drag a button onto the form and place the following code.
Imports System.Data.SqlClient
Public Class Form3 Inherits
System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
Private Sub Form3_Load(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;databas
e=pubs")
'you need to provide password for sql server
myConnection.Open()
myCommand = New SqlCommand("Delete from Authors
where city='Oakland'",_
myConnection)
'since no value is returned we use ExecuteNonQuery
ra=myCommand.ExecuteNonQuery()
MessageBox.Show("Records affected" & ra)
myConnection.Close()
End Sub
End Class
Updating Records
We will update a row in Authors table. Drag a button onto the form
and place the following code.
Imports System.Data.SqlClient
Public Class Form4 Inherits
System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
Private Sub Form4_Load(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;databas
e=pubs")
'you need to provide password for sql server
myConnection.Open()
myCommand = New SqlCommand("Update Authors Set
city='Oakland' where city=_
'San Jose' ",myConnection)
ra=myCommand.ExecuteNonQuery()
MessageBox.Show("Records affected" & ra)
myConnection.Close()
End Sub
End Class
Imports System.Data
02 Imports System.Data.SqlClient
03 Public Class account
06 Dim dr As SqlDataReader
08 dim ra as integer
12 myconnection.Open()
14 mycommand.ExecuteNonQuery()
16 myconnection.Close()
17 End Sub
18 End Class
'myconnection=conexiune
'comandasql=mycommand
'dr- citestedate
'
Imports System.Data
Imports System.Data.SqlClient
conexiune.Open()
comandasql.ExecuteNonQuery()
conexiune.Close()
End Sub
Imports System.Data
02 Imports System.Data.SqlClient
03
04 Public Class Form1
05 Dim con As SqlConnection
06 Dim cmd As SqlCommand
07 Dim dr As SqlDataReader
Private Sub insert_Click(ByVal sender As System.Object, ByVal
08
e As System.EventArgs) Handles insert.Click
cmd = New SqlCommand("insert into charl values(@Data1,@Data2)
09
", con)
10 cmd.Parameters.AddWithValue("@Data1", txt1.Text)
11 cmd.Parameters.AddWithValue("@Data2", txt2.Text)
12 cmd.ExecuteNonQuery()
13 MsgBox("Successfully inserted ")
14 End Sub
15
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
16
As System.EventArgs) Handles MyBase.Load
con = New
1
SqlConnection("server=ANONYMOUS;Trusted_Connection=yes;databa
7
se=charles")
1
con.Open()
8
19 End Sub
20 End Class