Connection to the database with App.config

Asked

Viewed 2,694 times

3

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

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 put in the Github for future reference.

Browser other questions tagged

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