C#C
C#3y ago
5 replies
Jonomuffin

❔ Connecting to a local DB

Packages Used:
Dapper v2.0.151
Microsoft.Extensions.Configuration.Abstractions 7.0.0
System.Data.SqlClient v4.8.5


// Define a minimal connection string
using System.Data;
using System.Data.SqlClient;
using Dapper;

string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=FretNautDB;Integrated Security=True;Connect Timeout=60;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";

try
{
    // Attempt to create a SqlConnection
    using IDbConnection connection = new SqlConnection(connectionString);
    {
        // Open the connection
        connection.Open();

        // Check if the connection is open
        if (connection.State == System.Data.ConnectionState.Open)
        {
            Console.WriteLine("Connection successfully opened.");
        }
        else
        {
            Console.WriteLine("Connection could not be opened.");
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();


System.ArgumentException: 'Keyword not supported: 'trust server certificate'.'

is thrown at the using statement so it doesn't even connect.

I published my local database using the SQL Server Database Project as a template and i'm able to query it through the SQL Server Object Explorer. The connection string I'm using was the one retrieved from the properties of the database.
Screenshot_2023-09-14_120125.png
gd.png
Was this page helpful?