Bonjour,
Suite a mon projet , je crée la connection a BDD sous MySql et j'ai une erreur qui me dit
Connection must be valid and open.
Je comprend pas pourquoi il me dit ca ?

Voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace WindowsApplication1
{   public class Projets
    {   
       private MySqlConnection connection;
 
       public Projets()
       {
           String ConnectionStr = "Database=projet;Data Source=localhost;User Id=root;Password=*****";
           MySqlConnection connection = new MySqlConnection(ConnectionStr);
           connection.Open();
       }
      /* public ~Projets()
       {
        connection.Close();
       }        
           */   
       public void ajouts(String nom,String Badge,String code)
        {            
            String actif = "Oui";
            String add = " INSERT INTO techniciens Values ("+nom+","+Badge+","+code+","+actif+")";
            MySqlCommand myCommand = new MySqlCommand(add,connection);
            myCommand.ExecuteNonQuery();
        }
} public partial class Form1 : Form
    {
        Projets Lien=new Projets();
        public Form1()
        {
            InitializeComponent();
        }
        public Form1(ref Projets lien)
        {
            Lien =  lien;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string texte1 = textBox1.Text.Trim();
            string texte2 = textBox2.Text.Trim();
            string texte3 = textBox3.Text.Trim();
 
            if ((texte1.Length != 0) && (texte2.Length != 0) && (texte3.Length != 0))
            {
                Lien.ajouts(texte1, texte2, texte3);
                MessageBox.Show("Ajout effectuer", "Vérification de la saisie",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Erreur sur une valeur saisie", "Vérification de la saisie",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
 
    }
}