I have a blazor app that intended for comparing our SQL Server databases.
In appsettings.json
, I'm trying to set:
"AllowedHosts": "*",
"ConnectionStrings": {
"SourceConnectionString": "Server=tcp:myserver.database.windows.net,1433;Initial Catalog=prod;Persist Security Info=False;User [email protected];Password=123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication='Active Directory Password';",
"TargetConnectionString": "Server=tcp:myserver.database.windows.net,1433;Initial Catalog=dev;Persist Security Info=False;User [email protected];Password=123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication='Active Directory Password';"
}
Note: SourceConnectionString
and TargetConnectionString
are used in different .cs
like below:
namespace MsSQLDatabaseComparison.DbContexts;
public class SourceDbContext
{
private readonly IConfiguration _configuration;
private readonly string _connectionString;
public SourceDbContext(IConfiguration configuration)
{
_configuration = configuration;
_connectionString = _configuration.GetConnectionString("SourceConnectionString");
}
public IDbConnection CreateConnection() => new SqlConnection(_connectionString);
}
namespace MsSQLDatabaseComparison.DbContexts;
public class TargetDbContext
{
private readonly IConfiguration _configuration;
private readonly string _connectionString;
public TargetDbContext(IConfiguration configuration)
{
_configuration = configuration;
_connectionString = _configuration.GetConnectionString("TargetConnectionString");
}
public IDbConnection CreateConnection() => new SqlConnection(_connectionString);
}
Then I got errors:
Any suggestions are much appreciate on how to fix the connection to SQL Server?
CodePudding user response:
Figured it out
using SqlConnection = Microsoft.Data.SqlClient.SqlConnection;
Much thx to @Daniel Mann and @AlwaysLearning for pointing it out
CodePudding user response:
Do you have access to log into the server? What do your options look like here? Do you use Active Directory Password, or do you use Windows Authentication, Sql Server Authentication?
Starting with how you connect manually will help answer how you can connect programmatically.
Here's a link to another question similar to yours: