SQLServer connection problem
Hi,
I made a program in c# .NET8.0 using EntityFrameworkCore to connect to an SQL Server. Everything work fines as long as I run the program from Visual Studio. Once compile and install on the computer (same computer I develop the program in VS) it won’t connect to the server.
I have added this code on a button to test the connection:
try
{
string? str = dbCtxt.Database.ProviderName;
bool cnx = dbCtxt.Database.CanConnect();
MessageBox.Show(“Provider: ” + str + “rnCan connect? ” + cnx, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
dbCtxt.Database.OpenConnection();
MessageBox.Show(“Connection opened”, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
dbCtxt.Database.CloseConnection();
}
catch (Microsoft.Data.SqlClient.SqlException ex)
{
MessageBox.Show($”Microsoft.Data.SqlClient.SqlException occurred: {ex.Message + “rnrn” + ex.StackTrace}”);
}
catch (DbUpdateException ex)
{
// Handle specific database update exception
MessageBox.Show($”DbUpdateException occurred: {ex.Message}”);
}
catch (DbException ex)
{
// Handle any other database exception
MessageBox.Show($”DbException occurred: {ex.Message}”);
}
catch (Exception ex)
{
// Catch any other exceptions
MessageBox.Show($”Exception occurred: {ex.Message + “rnrn” + ex.StackTrace}”);
tbError.Text = ex.StackTrace;
}
finally
{
//Here just to make sure it passes through this point…
MessageBox.Show(“Finally”, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
}
and I get this error message.
(Stack trace)
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.<>c.<OpenConnection>b__22_0(DatabaseFacade database)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.OpenConnection(DatabaseFacade databaseFacade)
at ****.frmMainMenu.btnFicheEmpAnnuel_Click(Object sender, RoutedEventArgs e) in C:Users….frmMainMenu.xaml.cs:line 99
I have tried multiple things, like changing the TargetPlatform from AnyCPU to x64, changing the Target OS Version, installing/deinstalling NuGET Microsoft.Data.SQLClient 5.2.1 … nothing works.
Anyone got a similar problem and solve it?
Regards
Hi,I made a program in c# .NET8.0 using EntityFrameworkCore to connect to an SQL Server. Everything work fines as long as I run the program from Visual Studio. Once compile and install on the computer (same computer I develop the program in VS) it won’t connect to the server.I have added this code on a button to test the connection: try
{
string? str = dbCtxt.Database.ProviderName;
bool cnx = dbCtxt.Database.CanConnect();
MessageBox.Show(“Provider: ” + str + “rnCan connect? ” + cnx, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
dbCtxt.Database.OpenConnection();
MessageBox.Show(“Connection opened”, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
dbCtxt.Database.CloseConnection();
}
catch (Microsoft.Data.SqlClient.SqlException ex)
{
MessageBox.Show($”Microsoft.Data.SqlClient.SqlException occurred: {ex.Message + “rnrn” + ex.StackTrace}”);
}
catch (DbUpdateException ex)
{
// Handle specific database update exception
MessageBox.Show($”DbUpdateException occurred: {ex.Message}”);
}
catch (DbException ex)
{
// Handle any other database exception
MessageBox.Show($”DbException occurred: {ex.Message}”);
}
catch (Exception ex)
{
// Catch any other exceptions
MessageBox.Show($”Exception occurred: {ex.Message + “rnrn” + ex.StackTrace}”);
tbError.Text = ex.StackTrace;
}
finally
{
//Here just to make sure it passes through this point…
MessageBox.Show(“Finally”, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
} and I get this error message.(Stack trace)at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.<>c.<OpenConnection>b__22_0(DatabaseFacade database)at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.OpenConnection(DatabaseFacade databaseFacade)at ****.frmMainMenu.btnFicheEmpAnnuel_Click(Object sender, RoutedEventArgs e) in C:Users….frmMainMenu.xaml.cs:line 99 I have tried multiple things, like changing the TargetPlatform from AnyCPU to x64, changing the Target OS Version, installing/deinstalling NuGET Microsoft.Data.SQLClient 5.2.1 … nothing works. Anyone got a similar problem and solve it?Regards Read More