Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Imports System.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

04 Dim myconnection As SqlConnection


05 Dim mycommand As SqlCommand

06 Dim dr As SqlDataReader

07 Dim dr1 As SqlDataReader

08 dim ra as integer

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


09
System.EventArgs) Handles Button6.Click
myconnection = New
10
SqlConnection("server=localhost;uid=sa;pwd=;database=medisam")
11 'you need to provide password for sql server

12 myconnection.Open()

mycommand = New SqlCommand("insert into account([ac],[actype],[msid],[ms])


values ('" & Label9.Text & "','" & ComboBox3.SelectedValue & "','" &
13
ComboBox1.SelectedValue & "','" & ComboBox2.SelectedValue & "')",
myconnection)

14 mycommand.ExecuteNonQuery()

15 'MessageBox.Show("New Row Inserted" & ra)

16 myconnection.Close()

17 End Sub

18 End Class

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


Button3.Click

'Public Class account

'myconnection=conexiune

'comandasql=mycommand
'dr- citestedate

'

Imports System.Data

Imports System.Data.SqlClient

Dim conexiune As SqlConnection

Dim comandasql As SqlCommand

Dim citestedate As SqlDataReader

Dim ra As Integer 'pt.un rand nou introdus

conexiune = New SqlConnection("server=HOME-


D16ABD96A8\PRIMAVERA;uid=mihaispr;pwd=mihai;database=Test")

conexiune.Open()

comandasql = New SqlCommand("insert into Noms([Id],[Nom]) values (' " &


Textbox1.Text & "','" & Textbox2.Text & " ')", conexiune)

comandasql.ExecuteNonQuery()

MessageBox.Show("New Row Inserted" & ra)

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

You might also like