-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSource.vb
More file actions
46 lines (39 loc) · 1.53 KB
/
Copy pathSource.vb
File metadata and controls
46 lines (39 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Option Explicit On
Option Strict On
Imports System.Data
Imports system.Data.SqlClient
Module Module1
Sub Main()
Dim connectionString As String = _
"Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)"
SqlCommandPrepareEx(connectionString)
Console.ReadLine()
End Sub
' <Snippet1>
Private Sub SqlCommandPrepareEx(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand("", connection)
' Create and prepare an SQL statement.
command.CommandText = _
"INSERT INTO Region (RegionID, RegionDescription) " & _
"VALUES (@id, @desc)"
Dim idParam As SqlParameter = _
New SqlParameter("@id", SqlDbType.Int, 0)
Dim descParam As SqlParameter = _
New SqlParameter("@desc", SqlDbType.Text, 100)
idParam.Value = 20
descParam.Value = "First Region"
command.Parameters.Add(idParam)
command.Parameters.Add(descParam)
' Call Prepare after setting the Commandtext and Parameters.
command.Prepare()
command.ExecuteNonQuery()
' Change parameter values and call ExecuteNonQuery.
command.Parameters(0).Value = 21
command.Parameters(1).Value = "Second Region"
command.ExecuteNonQuery()
End Using
End Sub
' </Snippet1>
End Module