Bonsoir a tous je suis un debutan, j'ai essayé de d'apprendre le c#, donc j'ai ecrit un code qui permet de lire une base de donnée access
mais en executant le commande ExecuteNonQuery une erreur Informations supplémentaires : ExecuteNonQuery : la propriété Connection n'a pas été initialisée.
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
      {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TPH\1.accdb");
        OleDbCommand cmd = new OleDbCommand();
 
        public Form1()
        {
            InitializeComponent();
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
        }
 
        public object TextBox1 { get; private set; }
 
        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable(); 
            con.Open();
           string query = "SELECT * FROM TPH WHERE 1=1";
 
            if (textBox1.Text == string.Empty & textBox2.Text == string.Empty & textBox3.Text == string.Empty)
            {
                MessageBox.Show("Veuiller saisir un Critère de recherche....!");
            }
            else
            {
                if (textBox1.Text.Length > 0)
                {
                    query = query + "and NUM LIKE '" + textBox1.Text + "%'";
 
                   }
 
                if (textBox2.Text.Length > 0)
                {
                    query = query + "and [NOM] LIKE '" + textBox2.Text + "%'";
                }
 
                if (textBox3.Text.Length > 0)
                {
                    query = query + "and [PRENOM] LIKE '" + textBox3.Text + "%'";
 
                }
 
                OleDbDataAdapter sda = new OleDbDataAdapter(query, con);
                sda.Fill(dt);
                 con.Close();
                dataGridView1.DataSource = dt;
                //   dataGridView1.AutoGenerateColumns = false;
 
            }
            textBox4.Text = dt.Rows.Count.ToString();
 
 
        }
 
        private void button1_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            dataGridView1.DataSource = "";
        }
 
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = "delete * from tph where num='" + textBox1.Text + "' and nom='" + textBox2.Text + "' and prenom ='" + textBox3.Text + "'";
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show(" Numeor Effacé ");
 
 
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand command = new
            OleDbCommand("update * from tph where num='" + textBox1.Text + "' and nom='" + textBox2.Text + "' and prenom ='" + textBox3.Text + "'", con);
           // cmd.CommandText = "update * from tph where num='" + textBox1.Text + "' and nom='" + textBox2.Text + "' and prenom ='" + textBox3.Text + "'";
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show(" Le Numéro a été bien mis à jour ");
        }
    }
 
}
j'ai pas pu trouver une reponse aide svp