internal static string GetConnectionString(string name) { // Get the connectionStrings section. System.Configuration.ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as System.Configuration.ConnectionStringsSection; ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings; // Get the connectionStrings key,value pairs collection. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); int i = 0; while (connectionStringsEnum.MoveNext()) { if (name == connectionStrings[i].Name) { return(connectionStrings[i].ConnectionString); } i += 1; } return(""); }
// Methods to access a section at runtime. // <Snippet2> // Show how to use the GetSection(string). // to access the connectionStrings section. static void GetConnectionStringsSection() { // Get the connectionStrings section. ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection; // Get the connectionStrings key,value pairs collection. ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and // display the connectionStrings key, value pairs. int i = 0; Console.WriteLine("[Display the connectionStrings]"); while (connectionStringsEnum.MoveNext()) { string name = connectionStrings[i].Name; Console.WriteLine("Name: {0} Value: {1}", name, connectionStrings[name]); i += 1; } Console.WriteLine(); }
} //end of checkSqlInjectionLowestSecurityForPassword() #endregion #region ****** 取得 Web.config 第一組連線字串的名稱 ****** /// <summary> /// 取得 Web.config 第一組連線字串的名稱 /// </summary> /// <returns></returns> public static String getFirstConnStrNameInWebConfig() { // Get the connectionStrings key,value pairs collection. ConnectionStringSettingsCollection connectionStrings = WebConfigurationManager.ConnectionStrings as ConnectionStringSettingsCollection; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and display the connectionStrings key, value pairs. //int i = 0; String strFirstConnStringName = ""; ////Response.Write("[Display connectionStrings]"); while (connectionStringsEnum.MoveNext()) { // i += 1; // if (i == 1) // { // // 傳回第一組連線字串的 name // strFirstConnStringName = connectionStrings[i].Name; //strTest = connectionStrings[0].Name; //LocalSqlServer strFirstConnStringName = connectionStrings[1].Name; //ConnStr_BIGUSER break; //stop while loop // } } //return connectionStrings[0].Name; return(strFirstConnStringName); } //end of getFirstConnStrNameInWebConfig()
public static List <String> GetDbConnectionList(string filter = "") { List <String> dbList = new List <string>(); if (string.IsNullOrEmpty(filter)) { filter = "V_"; } //foreach (ConnectionStringSettings item in System.Configuration.ConfigurationManager.ConnectionStrings) //{ // dbList.Add(item.Name); //} //return dbList; ConnectionStringSettingsCollection connections = GetConnectionStrings(); IEnumerator enumerator = connections.GetEnumerator(); while (enumerator.MoveNext()) { ConnectionStringSettings current = (ConnectionStringSettings)enumerator.Current; if (current.Name.StartsWith(filter)) { dbList.Add(current.Name); } } return(dbList); }
public ConnectionStringSettings GetConnectionString(string name) { // Get the connectionStrings section. ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection; // Get the connectionStrings key,value pairs collection. ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); int i = 0; while (connectionStringsEnum.MoveNext()) { if (connectionStrings[i].Name.Equals(name)) { return(connectionStrings[i]); } i++; } return(null); }
/// <summary> /// chester 獲取所有connectionStrings信息 /// </summary> /// <returns></returns> public static Hashtable GetConnectionStrings() { Hashtable hsconnectionStrings = new Hashtable(); ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings; IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); int i = 0; string name = ""; while (connectionStringsEnum.MoveNext()) { name = connectionStrings[i].Name; hsconnectionStrings.Add(name, connectionStrings[name].ToString()); i += 1; } return(hsconnectionStrings); }
/// <summary> /// 根据连接名称获取数据连接的字符串 /// </summary> /// <param name="connectName"></param> /// <returns></returns> public static string GetConnectionString(string connectName) { ConnectionStringSettings tempSetting = null; IEnumerator enumerator = AllConnections.GetEnumerator(); while (enumerator.MoveNext()) { tempSetting = enumerator.Current as ConnectionStringSettings; if (tempSetting == null) { continue; } if (tempSetting.Name == connectName) { return(tempSetting.ConnectionString); } } return(string.Empty); }
public static string GetDBConnectionString() { // The system databse connection string should be in the system // configuration file and should be called SilverTrackConnectionString string name = ""; try { // Get the connectionStrings. ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and search for valid connectionString int i = 0; while (connectionStringsEnum.MoveNext()) { name = connectionStrings[i++].Name; string value = connectionStrings[name].ToString(); if (name == "DBConnectionString") { return(value); } } } catch (Exception e) { log.Error("Failed to get the DBConnectionString connection string", e); throw e; } string mensaje = ""; mensaje = "Error de conexion a:" + name; throw new ConfigurationErrorsException(mensaje); }
// </Snippet2> // <Snippet5> // Show the use of GetSection(string, string). // to access the connectionStrings section. static void GetSection2() { try { // Get the connectionStrings section for the // specified Web app. This GetSection overload // can olny be called from within a Web application. ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings", "/configTest") as ConnectionStringsSection; // Get the connectionStrings key,value pairs collection ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and // display the connectionStrings key, value pairs. int i = 0; Console.WriteLine("[Display connectionStrings]"); while (connectionStringsEnum.MoveNext()) { string name = connectionStrings[i].Name; Console.WriteLine("Name: {0} Value: {1}", name, connectionStrings[name]); i += 1; } Console.WriteLine(); } catch (InvalidOperationException e) { string errorMsg = e.ToString(); Console.WriteLine(errorMsg); } }
// Show how to use ConnectionStrings. static void DisplayConnectionStrings() { // Get the ConnectionStrings collection. ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings; Console.WriteLine(); Console.WriteLine("Connection strings:"); // Loop to get the collection elements. IEnumerator conEnum = connections.GetEnumerator(); int i = 0; while (conEnum.MoveNext()) { string name = connections[i].Name; string connectionString = connections[name].ConnectionString; string provider = connections[name].ProviderName; Console.WriteLine("Name: {0}", name); Console.WriteLine("Connection string: {0}", connectionString); Console.WriteLine("Provider: {0}", provider); } }