Connecting to the database with App.config

3

How to establish a connection to the Northwind database using App.config and ADO.Net?

    
asked by anonymous 27.06.2014 / 02:48

1 answer

10

I think this is what you want:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings> 
    <add name="conn" connectionString="Data Source=.;Initial Catalog=Northwind;IntegratedSecurity=True" providerName="System.Data.SqlClient"/>  
  </connectionStrings>
</configuration>

Code:

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

public class Programa {
    public static void Main() {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        conn.Open();
    }
}

I placed it on GitHub for future reference .

    
27.06.2014 / 03:12