Code Snippet Details

<- Back to List


Language
C#.Net
Description
ConnectionStrings
Code
using System; namespace Alphabet.Framework.DAL { /// <summary> /// This class demonstrates a shaed class which does not need to be instantiated. /// </summary> /// <remarks>Using this class allows the Access Data Provider class to accept a connections string, /// and this in turn allows for it and the other data providers to have the same style of constructor.</remarks> public class ConnectionStrings { #region MS Access public enum AccessVersion : int { v2003 = 1, v2007 = 2 } internal class AccessConstants { internal class v2003 { internal const string ConnectionStringPrefix = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="; internal const string ConnectionStringSuffix = ";Jet OLEDB:Database Password="; } internal static class v2007 { internal const string ConnectionStringPrefix = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="; internal const string ConnectionStringSuffix = ";Jet OLEDB:Database Password="; } } public static string GetAccessConnectionString(AccessVersion accessVersion, string databaseLocation, string password) { switch (accessVersion) { case AccessVersion.v2003: return AccessConstants.v2003.ConnectionStringPrefix + databaseLocation + AccessConstants.v2003.ConnectionStringSuffix + password; case AccessVersion.v2007: return AccessConstants.v2007.ConnectionStringPrefix + databaseLocation + AccessConstants.v2007.ConnectionStringSuffix + password; default: throw new ArgumentException("Database version not supported"); } } #endregion #region SQL Server public enum SqlServerVersion : int { v2008 = 3 } public static string GetSqlServerConnectionString(SqlServerVersion version, string server, string database) { string connectionString = string.Empty; switch (version) { case SqlServerVersion.v2008: connectionString += "Data Source=" + server + ";"; connectionString += "Initial Catalog=" + database + ";"; connectionString += "Integrated Security=True"; return connectionString; default: throw new ArgumentException("Database version not supported"); } } public static string GetSqlServerConnectionString(SqlServerVersion version, string server, string database, string userId, string password) { string connectionString = string.Empty; switch (version) { case SqlServerVersion.v2008: connectionString += "Data Source=" + server + ";"; connectionString += "Initial Catalog=" + database + ";"; connectionString += "Persist Security Info=True;"; connectionString += "User ID=" + userId + ";"; connectionString += "Pwd=" + password; return connectionString; default: throw new ArgumentException("Database version not supported"); } } #endregion } }