© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago
Punica

SqlException without stopping or catching

Hello I get a Sql Exception in the following code when I connect to a MSSQL Server and make a Database. The Problem with it I can not catcvh the exception with my try block to see why that happening.

        private static void CreateGenericDatabase(DataBaseConfig dbBaseConfig)
        {
            try
            {
                string connectionString = DatabaseConnectionGen.GetConnectionString(dbBaseConfig.SqlServerIp, dbBaseConfig.SqlType, dbBaseConfig.Username, dbBaseConfig.Password, dbBaseConfig.WindowsAuth, "CoreDb");

                using TestDbContext dbContext = DatabaseConnectionGen.GenerateBuilder(dbBaseConfig.SqlType, connectionString);
                dbContext.Database.EnsureCreated();
                using IDbContextTransaction transaction = dbContext.Database.BeginTransaction();
                dbContext.Database.Migrate();
                transaction.Commit();
            }
            catch(SqlException ex)
            {
                // Detaillierte Protokollierung der SqlException
                Debug.WriteLine("SQL Exception:");
                Debug.WriteLine($"Message: {ex.Message}");
                Debug.WriteLine($"Error Code: {ex.ErrorCode}");
                Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
                if (ex.InnerException != null)
                {
                    Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }
                
                throw;
            }
            catch (Exception ex)
            {
                // Detaillierte Protokollierung anderer Ausnahmen
                Debug.WriteLine("Exception:");
                Debug.WriteLine($"Message: {ex.Message}");
                Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
                if (ex.InnerException != null)
                {
                    Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }

                throw; // Re-throw the exception to propagate it further
            }
        }
        private static void CreateGenericDatabase(DataBaseConfig dbBaseConfig)
        {
            try
            {
                string connectionString = DatabaseConnectionGen.GetConnectionString(dbBaseConfig.SqlServerIp, dbBaseConfig.SqlType, dbBaseConfig.Username, dbBaseConfig.Password, dbBaseConfig.WindowsAuth, "CoreDb");

                using TestDbContext dbContext = DatabaseConnectionGen.GenerateBuilder(dbBaseConfig.SqlType, connectionString);
                dbContext.Database.EnsureCreated();
                using IDbContextTransaction transaction = dbContext.Database.BeginTransaction();
                dbContext.Database.Migrate();
                transaction.Commit();
            }
            catch(SqlException ex)
            {
                // Detaillierte Protokollierung der SqlException
                Debug.WriteLine("SQL Exception:");
                Debug.WriteLine($"Message: {ex.Message}");
                Debug.WriteLine($"Error Code: {ex.ErrorCode}");
                Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
                if (ex.InnerException != null)
                {
                    Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }
                
                throw;
            }
            catch (Exception ex)
            {
                // Detaillierte Protokollierung anderer Ausnahmen
                Debug.WriteLine("Exception:");
                Debug.WriteLine($"Message: {ex.Message}");
                Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
                if (ex.InnerException != null)
                {
                    Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }

                throw; // Re-throw the exception to propagate it further
            }
        }


The connection string is fine and evrythings work as intended but I get in the output windows the following Exception.

"Microsoft.Data.SqlClient.SqlException" in Microsoft.Data.SqlClient.dll
and
"Microsoft.Data.SqlClient.SqlException" in Microsoft.EntityFrameworkCore.Relational.dll
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Entity Framework - SqlException
C#CC# / help
4y ago
❔ Microsoft.Data.SqlClient.SqlException
C#CC# / help
3y ago
✅ Catching exception at each function or globally?
C#CC# / help
3y ago
with or without ref
C#CC# / help
3y ago