Skip to content

Reorganize ADO.NET VB snippets #11534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
Imports System.Xml
Imports System.Data
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Windows.Forms



Public Class Form1
Inherits Form
Protected DataSet1 As DataSet
Protected dataGrid1 As DataGrid



Public Class Class1


' <Snippet1>
Public Sub CreateCommand()
Dim queryString As String = "SELECT * FROM Categories ORDER BY CategoryID"
Dim command As New SqlCommand(queryString)
command.CommandTimeout = 15
command.CommandType = CommandType.Text
End Sub
End Sub
' </Snippet1>
End Class
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Option Explicit On
Option Strict On

Imports System.Data
' <Snippet1>
Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Form1
' Add this code to the form's class:
Expand All @@ -30,7 +29,7 @@ Public Class Form1
' If you have not included "Asynchronous Processing=true" in the
' connection string, the command is not able
' to execute asynchronously.
Return "Data Source=(local);Integrated Security=true;" & _
Return "Data Source=(local);Integrated Security=true;" &
"Initial Catalog=AdventureWorks; Asynchronous Processing=true"
End Function

Expand All @@ -43,21 +42,21 @@ Public Class Form1
DisplayStatus("Ready")
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Private Sub Form1_FormClosing(ByVal sender As Object,
ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If isExecuting Then
MessageBox.Show(Me, "Cannot close the form until " & _
MessageBox.Show(Me, "Cannot close the form until " &
"the pending asynchronous command has completed. Please wait...")
e.Cancel = True
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
If isExecuting Then
MessageBox.Show(Me, _
"Already executing. Please wait until the current query " & _
MessageBox.Show(Me,
"Already executing. Please wait until the current query " &
"has completed.")
Else
Dim command As SqlCommand
Expand All @@ -69,11 +68,11 @@ Public Class Form1
' a few seconds before working with the data.
' This command does not do much, but that's the point--
' it does not change your data, in the long run.
Dim commandText As String = _
"WAITFOR DELAY '0:0:05';" & _
"UPDATE Production.Product SET ReorderPoint = ReorderPoint + 1 " & _
"WHERE ReorderPoint Is Not Null;" & _
"UPDATE Production.Product SET ReorderPoint = ReorderPoint - 1 " & _
Dim commandText As String =
"WAITFOR DELAY '0:0:05';" &
"UPDATE Production.Product SET ReorderPoint = ReorderPoint + 1 " &
"WHERE ReorderPoint Is Not Null;" &
"UPDATE Production.Product SET ReorderPoint = ReorderPoint - 1 " &
"WHERE ReorderPoint Is Not Null"

command = New SqlCommand(commandText, connection)
Expand Down Expand Up @@ -139,7 +138,7 @@ Public Class Form1

' You can create the delegate instance as you
' invoke it, like this:
Me.Invoke(New DisplayInfoDelegate(AddressOf DisplayStatus), _
Me.Invoke(New DisplayInfoDelegate(AddressOf DisplayStatus),
String.Format("Ready(last error: {0}", ex.Message))
Finally
isExecuting = False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Option Explicit On
Option Strict On

Imports System.Data
' <Snippet1>
Imports System.Data.SqlClient

Expand All @@ -12,11 +10,11 @@ Module Module1
' BeginExecuteNonQuery functionality.
' The WAITFOR statement simply adds enough time to prove the
' asynchronous nature of the command.
Dim commandText As String = _
"UPDATE Production.Product SET ReorderPoint = ReorderPoint + 1 " & _
"WHERE ReorderPoint Is Not Null;" & _
"WAITFOR DELAY '0:0:3';" & _
"UPDATE Production.Product SET ReorderPoint = ReorderPoint - 1 " & _
Dim commandText As String =
"UPDATE Production.Product SET ReorderPoint = ReorderPoint + 1 " &
"WHERE ReorderPoint Is Not Null;" &
"WAITFOR DELAY '0:0:3';" &
"UPDATE Production.Product SET ReorderPoint = ReorderPoint - 1 " &
"WHERE ReorderPoint Is Not Null"

RunCommandAsynchronously(commandText, GetConnectionString())
Expand All @@ -25,7 +23,7 @@ Module Module1
Console.ReadLine()
End Sub

Private Sub RunCommandAsynchronously( _
Private Sub RunCommandAsynchronously(
ByVal commandText As String, ByVal connectionString As String)

' Given command text and connection string, asynchronously execute
Expand All @@ -46,7 +44,7 @@ Module Module1
Threading.Thread.Sleep(100)
count += 1
End While
Console.WriteLine("Command complete. Affected {0} rows.", _
Console.WriteLine("Command complete. Affected {0} rows.",
command.EndExecuteNonQuery(result))
Catch ex As SqlException
Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message)
Expand All @@ -67,7 +65,7 @@ Module Module1
' If you have not included "Asynchronous Processing=true" in the
' connection string, the command is not able
' to execute asynchronously.
Return "Data Source=(local);Integrated Security=SSPI;" & _
Return "Data Source=(local);Integrated Security=SSPI;" &
"Initial Catalog=AdventureWorks; Asynchronous Processing=true"
End Function
End Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Option Strict On
Imports System.Data
' <Snippet1>
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Xml

Public Class Form1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
</ItemGroup>

</Project>
Loading