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:
- using System;: Imports the System namespace which contains fundamental classes and base classes.
- using System.Data.SqlClient;: Imports the namespace required to use SQL Server data provider classes.
- class Program: Declares a class named Program.
- static void Main(): Defines the entry point of the program.
- 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.
- string trustedConnectionString = “Server=myServerAddress;Database=myDataBase;Integrated Security=True;”;: Initializes a string variable for a connection string using Windows Authentication.
- 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.
- 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.
- 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.
- string encryptedConnectionString = “Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Encrypt=True;”;: Initializes a string variable for an encrypted SQL Server connection.
- 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.
- 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.
- 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.
- 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.
- 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.
- try: Begins a try block to catch any exceptions that occur during the connection attempt.
- connection.Open();: Attempts to open the connection to the SQL Server.
- Console.WriteLine(“Connection successful!”);: Prints a success message if the connection is opened successfully.
- catch (Exception ex): Begins a catch block to handle any exceptions.
- Console.WriteLine(“An error occurred: ” + ex.Message);: Prints an error message if an exception is caught.