Connect to a local database C #

0

What is the correct syntax for SqlConnection to connect to a local database? I think it is qlq thing of this kind !!

    SqlConnection liga = new SqlConnection(@"Data Source=(LocalDB)\v11.0;
                      AttachDbFilename=|DataDirectory|\BaseDeDados.mdf;
                      Integrated Security=True;
                      Connect Timeout=30");
    
asked by anonymous 26.12.2017 / 01:52

1 answer

0

Use this way, it is most recommended, using the keyword using .

var connectionString = @"Data Source=(LocalDB)\v11.0;
                      AttachDbFilename=|DataDirectory|\BaseDeDados.mdf;
                      Integrated Security=True;
                      Connect Timeout=30";

using (SqlConnection con = new SqlConnection(connectionString))
{
    con.Open();

    using (SqlCommand command = new SqlCommand("SELECT * FROM Table", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            // Leitura do seu Reader
        }
    }
}
    
26.12.2017 / 02:20