IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

commande ExecuteNonQuery c#


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2017
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Janvier 2017
    Messages : 15
    Billets dans le blog
    1
    Par défaut commande ExecuteNonQuery c#
    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

  2. #2
    Membre chevronné
    Avatar de nouanda
    Homme Profil pro
    Hobbyist
    Inscrit en
    Mai 2002
    Messages
    246
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Australie

    Informations professionnelles :
    Activité : Hobbyist

    Informations forums :
    Inscription : Mai 2002
    Messages : 246
    Par défaut
    Un objet OleDbCommand a besoin de savoir quelle connexion utiliser, mais tu ne le définis jamais.
    Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    cmd.Connection = con;
    cmd.CommandText = "delete * from tph where num='" + textBox1.Text + "' and nom='" + textBox2.Text + "' and prenom ='" + textBox3.Text + "'";
    con.Open();
    cmd.ExecuteNonQuery();

Discussions similaires

  1. Interception des commandes in et out
    Par KDD dans le forum x86 16-bits
    Réponses: 13
    Dernier message: 18/12/2002, 16h55
  2. [TP]besoin d'aide pour commandes inconnues
    Par Upal dans le forum Turbo Pascal
    Réponses: 15
    Dernier message: 03/10/2002, 10h48
  3. [Kylix] Commandes linux ss kylix
    Par csnickos dans le forum EDI
    Réponses: 3
    Dernier message: 15/09/2002, 20h24
  4. Réponses: 3
    Dernier message: 02/09/2002, 18h49
  5. Réponses: 2
    Dernier message: 11/08/2002, 21h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo