C
C#15mo ago
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
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();
// 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'.'
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.
4 Replies
Kouhai
Kouhai15mo ago
In the connection string, change Trust Server Certificate to TrustServerCertificate Also Multi Subnet Failover to MultiSubnetFailover
Jonomuffin
JonomuffinOP15mo ago
Revolved! The spaces for TrustServerCertificate, ApplicationIntent, and MultiSubnutFailover all needed to be removed. In my second photo is there a reason why the connection strings don't have the same keywords as https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-7.0 ?
SqlConnection.ConnectionString Property (System.Data.SqlClient)
Gets or sets the string used to open a SQL Server database.
Kouhai
Kouhai15mo ago
Sorry, I'm not sure what are you referring to 😅
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server