Probleme connectionString C#

Par gargotier, il y a 2 ans


Bonjour a tous, je reviens encore une fois vers vous pour un probleme de connection a une base de donnees en C#. J'ai ce message d'erreur quand je tente de faire la connectino a la db "The ConnectionString property has not been initialized". En utilisant le debugger j'ai reussi a identifier la ligne de code qui pose probleme => command.Connection.Open(); mais je n'arrive pas a corriger le probleme.

Je travaille sur un projet windows forms en utilisant Ms Sql Server en local. Voici le code en question:

private void button1_Click(object sender, EventArgs e) { using (SqlConnection connection = new SqlConnection()) { string connectionString = null; connectionString = (@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=mioProgetto; Integrated Security=True;Connect Timeout=30;Encrypt=False; TrustServerCertificate=False;ApplicationIntent= ReadWrite;MultiSubnetFailover=False"); SqlConnection cnn; string sql = "SELECT data, totale from dbo.restaurant group by data"; cnn = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(sql, connection); command.Connection.Open(); SqlDataReader reader = command.ExecuteReader(); try { cnn.Open(); MessageBox.Show("Connection Open ! "); cnn.Close(); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } }```

Aucune réponse