ConnectionStrings Propriedade
Recolher sumário
Expandir sumário
Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

ConfigurationManager.ConnectionStrings Propriedade

 

Gets the ConnectionStringsSection data for the current application's default configuration.

Namespace:   System.Configuration
Assembly:  System.Configuration (em System.Configuration.dll)

public static ConnectionStringSettingsCollection ConnectionStrings { get; }

Valor de propriedade

Type: System.Configuration.ConnectionStringSettingsCollection

Returns a ConnectionStringSettingsCollection object that contains the contents of the ConnectionStringsSection object for the current application's default configuration.

Exception Condition
ConfigurationErrorsException

Could not retrieve a ConnectionStringSettingsCollection object.

A ConnectionStringsSection object contains the contents of the configuration file's connectionStrings section.

The following example shows how to use a connection string to read data from a database.

using System;
using System.Configuration;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadProducts();
        }

        static void ReadProducts()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["WingtipToys"].ConnectionString;
            string queryString = "SELECT Id, ProductName FROM dbo.Products;";
            using (var connection = new SqlConnection(connectionString))
            {
                var command = new SqlCommand(queryString, connection);
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
                    }
                }
            }
        }
    }
}

The previous example assumes your project has an App.config as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <connectionStrings>
      <add name="WingtipToys" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False" />
    </connectionStrings>
</configuration>

.NET Framework
Disponível desde 2.0
Retornar ao topo
Mostrar:
© 2016 Microsoft