App.config to access an SQL Server database?

Asked

Viewed 878 times

2

How can I adjust this app.config to access my SQL Server database?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IServidor" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:2666/Servidor.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IServidor" contract="ServidorReference.IServidor"
                name="BasicHttpBinding_IServidor" />
        </client>
    </system.serviceModel>
</configuration>

3 answers

0

To access your SQL Server, your app.config has to have connection strings

<connectionStrings>
  <add name="NomeDaStringDeConexao" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLExpress; Initial Catalog=SeuBanco;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>

0

Here’s a full example of the.config app:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>    
  <connectionStrings>
    <add name="SqlServerConnString" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True;MultipleActiveResultSets=True" />
  </connectionStrings>
</configuration>

To return the string of the app.config use:

var connstring = ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString;

These examples I extracted from a repository of mine from Github, you can check how it is done.

https://github.com/thiagoloureiro/Dapper_ComplexObjects/

0

You can add a Connectionstring. Right after the tag startup in his App.config you can add:

    <connectionStrings>
        <add name="ConexaoSQLSERVER" 
             connectionString="Data Source=EnderecoServer; 
                               Initial Catalog=NomedaDB; 
                               Integrated Security=True;" 
             providerName="System.Data.SqlClient"/>
    </connectionStrings>

If you want to use a Localdb and want to attack a bank in a directory, your connectionString look like this:

Server=.\\SQLExpress;AttachDbFilename=C:\\Caminho até a DB\\MinhaDB.mdf;Database=NomedaDB;
Trusted_Connection=Yes;

To attack DB in the project folder:

Server=.\\SQLExpress;AttachDbFilename=|DiretoriodoProjeto|suadb.mdf;Database=NomedaDB;
Trusted_Connection=Yes;

Before you start building the connection note to forget to add the reference that handles the App.config.

Add referências System.Configuration

Add referências System.Configuration

In the project create a separate class to handle the connection: Construção da Classe

An example of using Conexaodb: Ultilizacão da ConexaoDB

Browser other questions tagged

You are not signed in. Login or sign up in order to post.