SQL Connection String for Desktop applications C# and .NET

using System;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        // Basic Connection String
        // This connects to the SQL Server using a specified server address, database name, username, and password.
        string basicConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";

        // Trusted Connection (Integrated Security)
        // This connects to the SQL Server using Windows Authentication (integrated security).
        string trustedConnectionString = "Server=myServerAddress;Database=myDataBase;Integrated Security=True;";

        // Connecting to a Named Instance
        // This connects to a specific named instance of SQL Server.
        string namedInstanceConnectionString = "Server=myServerAddress\\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;";

        // Connecting with a Specific Port
        // This connects to the SQL Server using a specified port number.
        string portConnectionString = "Server=myServerAddress,myPortNumber;Database=myDataBase;User Id=myUsername;Password=myPassword;";

        // Connection with Connection Timeout
        // This specifies a timeout period (in seconds) for establishing the connection.
        string timeoutConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Connection Timeout=30;";

        // Encrypted Connection
        // This enables encryption for the connection to SQL Server.
        string encryptedConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Encrypt=True;";

        // Connection String with Multiple Active Result Sets (MARS) Enabled
        // This allows multiple active result sets to be used in a single connection.
        string marsConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;MultipleActiveResultSets=True;";

        // Asynchronous Processing Enabled
        // This enables asynchronous processing of commands.
        string asyncConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Asynchronous Processing=True;";

        // Connecting with Application Name
        // This specifies the application name to be used in the connection string.
        string appNameConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Application Name=MyAppName;";

        // Connection with Failover Partner (for Database Mirroring)
        // This specifies a failover partner server for database mirroring.
        string failoverPartnerConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Failover Partner=myMirrorServerAddress;";

        // Example of using a connection string
        // Here, the basic connection string is used to create a new SqlConnection object.
        using (SqlConnection connection = new SqlConnection(basicConnectionString))
        {
            try
            {
                // Attempt to open the connection.
                connection.Open();
                // If successful, print a success message.
                Console.WriteLine("Connection successful!");
            }
            catch (Exception ex)
            {
                // If an error occurs, print the error message.
                Console.WriteLine("An error occurred: " + ex.Message);
            }
        }
    }
}

Explanation of Each Line:

  1. using System;: Imports the System namespace which contains fundamental classes and base classes.
  2. using System.Data.SqlClient;: Imports the namespace required to use SQL Server data provider classes.
  3. class Program: Declares a class named Program.
  4. static void Main(): Defines the entry point of the program.
  5. string basicConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;”;: Initializes a string variable with a basic SQL Server connection string using a server address, database name, username, and password.
  6. string trustedConnectionString = “Server=myServerAddress;Database=myDataBase;Integrated Security=True;”;: Initializes a string variable for a connection string using Windows Authentication.
  7. string namedInstanceConnectionString = “Server=myServerAddress\\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;”;: Initializes a string variable for connecting to a named instance of SQL Server.
  8. string portConnectionString = “Server=myServerAddress,myPortNumber;Database=myDataBase;User Id=myUsername;Password=myPassword;”;: Initializes a string variable for connecting to SQL Server using a specific port number.
  9. string timeoutConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Connection Timeout=30;”;: Initializes a string variable for a connection string with a connection timeout period.
  10. string encryptedConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Encrypt=True;”;: Initializes a string variable for an encrypted SQL Server connection.
  11. string marsConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;MultipleActiveResultSets=True;”;: Initializes a string variable for a connection string with Multiple Active Result Sets (MARS) enabled.
  12. string asyncConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Asynchronous Processing=True;”;: Initializes a string variable for a connection string with asynchronous processing enabled.
  13. string appNameConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Application Name=MyAppName;”;: Initializes a string variable for a connection string specifying an application name.
  14. string failoverPartnerConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Failover Partner=myMirrorServerAddress;”;: Initializes a string variable for a connection string with a failover partner for database mirroring.
  15. using (SqlConnection connection = new SqlConnection(basicConnectionString)): Creates a new SqlConnection object using the basic connection string and ensures the connection is properly closed after use.
  16. try: Begins a try block to catch any exceptions that occur during the connection attempt.
  17. connection.Open();: Attempts to open the connection to the SQL Server.
  18. Console.WriteLine(“Connection successful!”);: Prints a success message if the connection is opened successfully.
  19. catch (Exception ex): Begins a catch block to handle any exceptions.
  20. Console.WriteLine(“An error occurred: ” + ex.Message);: Prints an error message if an exception is caught.