/// <summary> /// Get a SMO Server object that is connected to the connection /// </summary> /// <param name="ci">Conenction info</param> /// <returns>Smo Server object for the connection</returns> public static Microsoft.SqlServer.Management.Smo.Server GetSmoServer(IManagedConnection mc) { SqlOlapConnectionInfoBase ci = mc.Connection; if (ci == null) { throw new ArgumentNullException("ci"); } SMO.Server server = null; // see what type of connection we have been passed SqlConnectionInfoWithConnection ciWithCon = ci as SqlConnectionInfoWithConnection; if (ciWithCon != null) { server = new SMO.Server(ciWithCon.ServerConnection); } else { SqlConnectionInfo sqlCi = ci as SqlConnectionInfo; if (sqlCi != null) { server = new SMO.Server(new ServerConnection(sqlCi)); } } if (server == null) { throw new InvalidOperationException(); } return(server); }
/// <summary> /// returns SqlConnectionInfoWithConnection object constructed using our internal vars /// </summary> /// <returns></returns> private SqlConnectionInfoWithConnection GetTempSqlConnectionInfoWithConnection( string serverName, bool trusted, string userName, SecureString password, string databaseName) { SqlConnectionInfoWithConnection tempCI = new SqlConnectionInfoWithConnection(serverName); tempCI.SingleConnection = false; tempCI.Pooled = false; //BUGBUG - set the right application name? if (trusted) { tempCI.UseIntegratedSecurity = true; } else { tempCI.UseIntegratedSecurity = false; tempCI.UserName = userName; tempCI.SecurePassword = password; } tempCI.DatabaseName = databaseName; return(tempCI); }
/// <summary> /// contructs the object and initializes its SQL ConnectionInfo and ServerConnection properties /// using the specified connection info containing live connection. /// </summary> /// <param name="ciObj">connection info containing live connection</param> public CDataContainer(object ciObj, bool ownConnection) { SqlConnectionInfoWithConnection ci = (SqlConnectionInfoWithConnection)ciObj; if (ci == null) { throw new ArgumentNullException("ci"); } ApplyConnectionInfo(ci, ownConnection); }
/// <summary> /// MUST be called, as we'll be closing SQL connection inside this call /// </summary> private void Dispose(bool disposing) { try { //take care of live SQL connection if (this.sqlCiWithConnection != null) { this.sqlCiWithConnection.ConnectionClosed -= new EventHandler(OnSqlConnectionClosed); if (disposing) { //if we have the managed connection interface, then use it to disconnect. //Otherwise, Dispose on SqlConnectionInfoWithConnection should disconnect if (this.managedConnection != null) { //in this case we have gotten sqlCiWithConnection as this.managedConnection.Connection if (this.ownConnection) { this.managedConnection.Close(); } this.managedConnection = null; } else { if (this.ownConnection) { this.sqlCiWithConnection.Dispose();//internally will decide whether to disconnect or not } } this.sqlCiWithConnection = null; } else { this.managedConnection = null; this.sqlCiWithConnection = null; } } else if (this.managedConnection != null) { if (disposing && this.ownConnection) { this.managedConnection.Close(); } this.managedConnection = null; } } catch (Exception) { } }
/// <summary> /// stores specified connection info and performs some extra initialization steps /// that can only be done after we have the connection information /// </summary> /// <param name="ci"></param> private void ApplyConnectionInfo(SqlOlapConnectionInfoBase ci, bool ownConnection) { this.connectionInfo = ci; this.ownConnection = ownConnection; //cache the cast value. It is OK that it is null for non SQL types this.sqlCiWithConnection = ci as SqlConnectionInfoWithConnection; if (this.sqlCiWithConnection != null) { //we want to be notified if it is closed this.sqlCiWithConnection.ConnectionClosed += new EventHandler(OnSqlConnectionClosed); } }
/// <summary> /// Close the current connection if applicable. /// </summary> public void Close() { if (this.closed) { return; } if (this.closeOnDispose) { IDisposable disp = this.connection as IDisposable; if (disp != null) { disp.Dispose(); } } else { // if we are not closing the connection and it is a sql connection then ensure it // is left in the master database. SqlConnectionInfoWithConnection sqlConnection = this.connection as SqlConnectionInfoWithConnection; if (sqlConnection != null && sqlConnection.ServerConnection.DatabaseEngineType == DatabaseEngineType.Standalone) { try { sqlConnection.ServerConnection.ExecuteNonQuery("use [master]"); } // don't error if this fails catch { } } } if (this.connectionAddedToActiveConnections) { lock (ActiveConnections) { ActiveConnections.Remove(SharedConnectionUtil.GetConnectionKeyName(connection)); } } this.connection = null; this.closed = true; }
/// <summary> /// /// </summary> /// <param name="dataContainer">Data container</param> /// <param name="xmlParameters">XML string with parameters</param> public CDataContainer(CDataContainer dataContainer, string xmlParameters) { Server = dataContainer.Server; this.serverName = dataContainer.serverName; this.serverType = dataContainer.serverType; this.connectionInfo = dataContainer.connectionInfo; this.ownConnection = dataContainer.ownConnection; this.sqlCiWithConnection = dataContainer.connectionInfo as SqlConnectionInfoWithConnection; if (this.sqlCiWithConnection != null) { //we want to be notified if it is closed this.sqlCiWithConnection.ConnectionClosed += new EventHandler(OnSqlConnectionClosed); } if (xmlParameters != null) { XmlDocument doc = GenerateXmlDocumentFromString(xmlParameters); this.Init(doc); } }
/// <summary> /// Create a data container object /// </summary> /// <param name="connInfo">connection info</param> /// <param name="databaseExists">flag indicating whether to create taskhelper for existing database or not</param> internal static CDataContainer CreateDataContainer( ConnectionInfo connInfo, bool databaseExists = false, XmlDocument containerDoc = null) { if (containerDoc == null) { containerDoc = CreateDataContainerDocument(connInfo, databaseExists); } var serverConnection = ConnectionService.OpenServerConnection(connInfo, "DataContainer"); var connectionInfoWithConnection = new SqlConnectionInfoWithConnection(); connectionInfoWithConnection.ServerConnection = serverConnection; CDataContainer dataContainer = new CDataContainer(ServerType.SQL, connectionInfoWithConnection, true); dataContainer.Init(containerDoc); return(dataContainer); }
/// <summary> /// contructs the object and initializes its SQL ConnectionInfo and ServerConnection properties /// using the specified connection info containing live connection. /// /// in addition creates a server of the given server type /// </summary> /// <param name="ci">connection info containing live connection</param> public CDataContainer(ServerType serverType, object ciObj, bool ownConnection) { SqlConnectionInfoWithConnection ci = (SqlConnectionInfoWithConnection)ciObj; if (ci == null) { throw new ArgumentNullException("ci"); } this.serverType = serverType; ApplyConnectionInfo(ci, ownConnection); if (serverType == ServerType.SQL) { //NOTE: ServerConnection property will constuct the object if needed m_server = new Server(ServerConnection); } else { throw new ArgumentException("SRError.UnknownServerType(serverType.ToString()), serverType"); } }