How do I connect to SQL Server 2022 using VB.NET Core Desktop

Marc Menzel 121 Reputation points
2025-06-04T03:08:04.02+00:00

How do I connect to SQL Server 2022 using VB.NET Core Desktop. Should I use a connection string such as "Server=localhost;Database=CombinedCommands;Trusted_Connection=True;TrustServerCertificate=True" and should I install an SSL certificate for SQL Server 2022? I am using the Microsoft.Data.SqlClient namespace provided by the Microsoft.Data.SqlClient NuGET Package version 6.0.2

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,264 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 49,005 Reputation points MVP Volunteer Moderator
    2025-06-04T03:16:33.9666667+00:00

    The following works for local development without requiring SSL certificate installation:

    "Server=localhost;Database=CombinedCommands;Trusted_Connection=True;TrustServerCertificate=True"
    

    Trusted_Connection=True uses Windows Authentication. TrustServerCertificate=True tells the client to ignore certificate validation (typically okay for dev/test, not for production).

    For production environments, you should use certs. SQL Server supports TLS encryption for connections. To implement this:

    • Install a valid SSL certificate on SQL Server (trusted by clients), OR
    • Use Encrypt=False (not recommended), OR
    • Use TrustServerCertificate=True (not recommended for production).

    Here's a sample code illustrating the use of Microsoft.Data.SqlClient:

    Imports Microsoft.Data.SqlClient
    
    Module Program
        Sub Main()
            Dim connectionString As String =
                "Server=localhost;Database=CombinedCommands;Trusted_Connection=True;TrustServerCertificate=True"
    
            Using connection As New SqlConnection(connectionString)
                Try
                    connection.Open()
                    Console.WriteLine("Connection successful.")
                Catch ex As Exception
                    Console.WriteLine("Connection failed: " & ex.Message)
                End Try
            End Using
        End Sub
    End Module
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.