/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks ConnectionStringSettings.RetrieveFromConfigByProvider/CS/source.cs
37 строк
1 KB
Bill Wagner
Update samples (#38401)
27 ноя 2023, 18:05
Не верифицирован
27 ноя 2023, 18:05
0eadf05
Код
Авторство
О чём код?
using System; using System.Configuration; static class Program { static void Main() { var s = GetConnectionStringByProvider("System.Data.SqlClient"); Console.WriteLine(s); Console.ReadLine(); } // <Snippet1> // Retrieve a connection string by specifying the providerName. // Assumes one connection string per provider in the config file. static string? GetConnectionStringByProvider(string providerName) { // Get the collection of connection strings. ConnectionStringSettingsCollection? settings = ConfigurationManager.ConnectionStrings; // Walk through the collection and return the first // connection string matching the providerName. if (settings != null) { foreach (ConnectionStringSettings cs in settings) { if (cs.ProviderName == providerName) { return cs.ConnectionString; } } } return null; } // </Snippet1> }