using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sdaleo.Systems.SQLServer { /// /// Handles SQL Server Credentials, to be /// used by all our SQL Server Functions /// public class SQLServerCredential : IComparable, ICloneable, IConnectDb, IamDBMS, IsupportTimeouts { #region IConnectDb /// /// Let Callers know this is a SQL Server Connection Object /// public DBSystem DBType { get { return DBSystem.SQL_SERVER; } } /// /// Check to see if the Credential Object consists of valid input /// public bool IsValid { get { if (!Validation.IsValidDataSource(_UDL.DataSource)) return false; if (!_UDL.TrustedConnection && !ValidationConsts.Generic.IsValidUserCredential(_UDL.Username, _UDL.Password)) return false; if (!String.IsNullOrEmpty(_UDL.DataBase) && IsDatabaseSet) return false; return true; } } /// /// /// public string ConnectionString { get { return _UDL.ConnectionString; } } /// /// SQL Server uses Server N' Instance /// public string DataSource { get { return _UDL.DataSource; } } /// /// SQL Server requires a user /// public string User { get { return _UDL.Username; } } /// /// SQL Server supports is a DBMS /// public bool SupportsDBMS { get { return true; } } /// /// SQL Server is a DBMS /// public IamDBMS DBMS { get { return this; } } /// /// SQL Server supports Timeouts /// public bool SupportsTimeouts { get { return true; } } /// /// SQL Server supports Timeouts /// public IsupportTimeouts Timeouts { get { return this; } } #endregion private UDL _UDL = null; #region SQL Server Credential Constructors /// /// Create a SQL Server Connection from an UDL Object /// /// internal SQLServerCredential(UDL udl) { if (udl != null) _UDL = udl; } /// /// Create an Untrusted SQLServer Credential with an Initial DB Specified /// public SQLServerCredential(string strServer, string strInstance, string strDatabase, string strUser, string strPassword) { _UDL = new UDL(strDatabase, strUser, strPassword, strServer, strInstance); } /// /// Create an Untrusted SQLServer Credential with no Initial DB Specified /// public SQLServerCredential(string strServer, string strInstance, string strUser, string strPassword) { _UDL = new UDL(String.Empty, strUser, strPassword, strServer, strInstance); } /// /// Create a Trusted SQLServer Credential with an Initial DB Specified /// public SQLServerCredential(string strServer, string strInstance, string strDatabase) { _UDL = new UDL(strDatabase, String.Empty, String.Empty, strServer, strInstance, true); } /// /// Create a Trusted SQLServer Credential with no Initial DB Specified /// public SQLServerCredential(string strServer, string strInstance) { _UDL = new UDL(String.Empty, String.Empty, String.Empty, strServer, strInstance, true); } /// /// Create a Trusted SQLServer Credential with SQLExpress on the Local Machine to Attach a DB File (Creates a User Instance) /// /// public SQLServerCredential(string strAttachSQLDBFile) { _UDL = new UDL(String.Empty, String.Empty, String.Empty, String.Empty, "SQLEXPRESS", strAttachSQLDBFile, true, false); } #endregion #region IamDBMS public string Server { get { return _UDL.ServerAddress; } } public string Instance { get { return _UDL.InstanceName; } } public string Database { get { return _UDL.DataBase; } } public bool IsDatabaseSet { get { DBError dbError = Validation.IsValidDatabaseName(Database); return !dbError.ErrorOccured; } } public bool IsDatabaseSetAndNonSystem { get { if (IsDatabaseSet && !SQLServerUtilities.IsSystemDatabaseName(Database)) return true; return false; } } public bool IsDatabaseSetAndNonDefault { get { if (IsDatabaseSet && !SQLServerUtilities.IsDefaultDatabaseName(Database)) return true; return false; } } /// /// Use this to quickly create a new SQLServerCredential with a different DatabaseName /// /// Name of Database To Set /// a new SQLServerCredential Object public IConnectDb WithDatabase(string strDatabaseName) { // Create a new Credential Object and change the DatabaseName SQLServerCredential credential = (SQLServerCredential)Clone(); credential._UDL.DataBase = strDatabaseName; return credential; } /// /// Use this to quickly create a new SQLServerCredential with no DatabaseName defined /// /// a new SQLServerCredential Object public IConnectDb WithoutDatabase() { return WithDatabase(String.Empty); } #endregion #region IsupportTimeouts public const int DEFAULT_COMMAND_TIMEOUT = 45; private uint _CommandTimeout = DEFAULT_COMMAND_TIMEOUT; public uint CommandTimeout { get { return _CommandTimeout; } set { _CommandTimeout = value; } } public uint ConnectionTimeout { get { return _UDL.ConnectionTimeout; } set { _UDL.ConnectionTimeout = value; } } /// /// Use this to quickly create a new SQLServerCredential with a different connection timeout /// /// Specify the Connection Timeout /// a new SQLServerCredential Object public IConnectDb WithConnectionTimeout(uint nConnectionTimeout) { // Create a new Credential Object and change the Timeouts SQLServerCredential credential = (SQLServerCredential)Clone(); credential.ConnectionTimeout = nConnectionTimeout; return credential; } /// /// Use this to quickly create a new SQLServerCredential with a different command timeout /// /// Specify the Command Timeout /// a new SQLServerCredential Object public IConnectDb WithCommandTimeout(uint nCommandTimeout) { // Create a new Credential Object and change the Timeouts SQLServerCredential credential = (SQLServerCredential)Clone(); credential.CommandTimeout = nCommandTimeout; return credential; } /// /// Use this to quickly create a new SQLServerCredential with different Timeouts /// /// Specify the Connection Timeout /// Specify the Command Timeout /// a new SQLServerCredential Object public IConnectDb WithTimeouts(uint nConnectionTimeout, uint nCommandTimeout) { // Create a new Credential Object and change the Timeouts SQLServerCredential credential = (SQLServerCredential)Clone(); credential.ConnectionTimeout = nConnectionTimeout; credential.CommandTimeout = nCommandTimeout; return credential; } #endregion #region ICloneable Members public object Clone() { SQLServerCredential credential = new SQLServerCredential((UDL)this._UDL.Clone()); credential.CommandTimeout = this.CommandTimeout; return credential; } #endregion #region IComparable Members public int CompareTo(object obj) { SQLServerCredential otherCredential = obj as SQLServerCredential; if (otherCredential != null) { int nCompare = _UDL.CompareTo(otherCredential._UDL); return nCompare; } else { throw new ArgumentException("Object is not a SQLServerCredential"); } } #endregion } }