private DatabaseConfigurator CreateDatabaseConfigurator(IUnityContainer container) { var currentConnectionName = this.connectionName; var configurationSource = ConfigurationSourceFactory.Create(); if (string.IsNullOrEmpty(currentConnectionName)) { currentConnectionName = DatabaseConfigurator.DefaultDatabaseConnectionName; var settings = DatabaseSettings.GetDatabaseSettings(configurationSource); if (settings != null) { currentConnectionName = settings.DefaultDatabase; } } var section = (ConnectionStringsSection)configurationSource.GetSection("connectionStrings"); Contract.Assert(section != null); var css = section.ConnectionStrings[currentConnectionName]; Contract.Assert(css != null); Contract.Assert(!string.IsNullOrEmpty(css.ProviderName)); var configurator = container.Resolve <DatabaseConfigurator>(css.ProviderName); configurator.ConnectionString = css.ConnectionString; return(configurator); }
public SqlDependencyExpiration(string commandText, CommandType commandType, string connectionStringName, IDictionary <string, object> parameters) { if (string.IsNullOrEmpty(connectionStringName)) { this.ConnectionName = DatabaseSettings.GetDatabaseSettings(ConfigurationSourceFactory.Create()).DefaultDatabase; } else { this.ConnectionName = connectionStringName; } //if (!StartedConnectionNames.Contains(this.ConnectionName)) //{ bool success = SqlDependency.Start(ConfigurationManager.ConnectionStrings[this.ConnectionName].ConnectionString); // if (success) // { // StartedConnectionNames.Add(this.ConnectionName); // } // else // { // throw new Exception("已存在兼容的侦听器:" + this.ConnectionName); // } //} using (SqlConnection sqlConnection = DatabaseFactory.CreateDatabase(this.ConnectionName).CreateConnection() as SqlConnection) { SqlCommand command = new SqlCommand(commandText, sqlConnection); command.CommandType = commandType; if (parameters != null) { this.AddParameters(command, parameters); } SqlDependency dependency = new SqlDependency(command); dependency.OnChange += delegate { this.HasChanged = true; if (this.Expired != null) { this.Expired(this, new EventArgs()); } }; if (sqlConnection.State != ConnectionState.Open) { sqlConnection.Open(); } command.ExecuteNonQuery(); } }
/// <summary> /// Opens the connection strings section, builds the design time nodes and adds them to the application node. /// </summary> /// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param> /// <param name="rootNode">The root node of the application.</param> /// <param name="section">The <see cref="ConfigurationSection"/> that was opened from the <see cref="IConfigurationSource"/>.</param> protected override void OpenCore(IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, ConfigurationSection section) { if (null != section) { string defaultDatabase = string.Empty; DatabaseSettings databaseSection = DatabaseSettings.GetDatabaseSettings(GetConfigurationSource(serviceProvider)); if (null != databaseSection) { defaultDatabase = databaseSection.DefaultDatabase; } DatabaseSectionNode node = rootNode.Hierarchy.FindNodeByType(typeof(DatabaseSectionNode)) as DatabaseSectionNode; if (null == node) { AddDatabaseSectionNodeCommand dbCmd = new AddDatabaseSectionNodeCommand(serviceProvider, false); dbCmd.Execute(rootNode); node = dbCmd.ChildNode as DatabaseSectionNode; Debug.Assert(node != null); } ConnectionStringsSectionNodeBuilder builder = new ConnectionStringsSectionNodeBuilder(serviceProvider, (ConnectionStringsSection)section, defaultDatabase, node); node.AddNode(builder.Build()); } }