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

Windows Forms Discussion :

Insert les donnees dans la base en C#


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 46
    Par défaut Insert les donnees dans la base en C#
    Bonjour

    En fait j'ai une winform et j'aimerai inserer des donnees dans ma base
    j'utilise le mode deconnecté je vous montre mon code la precision la partie SELECT marche bien c'est l'INSERT qui ne marche pas

    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
     
     
     private void button_ajouter_Click(object sender, EventArgs e)
            {
                //on va utiliser un Insert
                myDa.InsertCommand = new SqlCommand();
                myDa.InsertCommand.Connection = connection;
                myDa.InsertCommand.CommandText = "INSERT INTO T_AVION(AV_MODELE, AV_CONSTRUCTEUR) VALUES('" + txt_model + "','" + txt_construc + "')";
     
     
                myDa.Update(myDs, "T_AVION");
                dataGridView1.DataSource =myDs.Tables["T_AVION"];
     
     
     
     
    private void button_affichage_Click(object sender, EventArgs e)
            {
                //on va utiliser la fonction select
                myDa.SelectCommand = new SqlCommand();
                myDa.SelectCommand.Connection = connection;
                myDa.SelectCommand.CommandText = "SELECT * FROM T_AVION";
     
                myDa.Fill(myDs, "T_AVION");
                dataGridView1.DataSource = myDs.Tables["T_AVION"];
     
            }
     
    Si quelqu'un à une idee merci

  2. #2
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 46
    Par défaut
    je viens de remarquer que cette ligne provoque un bug

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    myDa.Update(myDs, "T_AVION");
    donc voila le nouveau 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
     
    private void button_ajouter_Click(object sender, EventArgs e)
            {
                //on va utiliser un Insert
                myDa.InsertCommand = new SqlCommand();
                myDa.InsertCommand.Connection = connection;
                myDa.InsertCommand.CommandText = "INSERT INTO T_AVION(AV_MODELE, AV_CONSTRUCTEUR) VALUES('" + txt_model + "','" + txt_construc + "')";
     
                MessageBox.Show("information saisie");
     
                dataGridView1.DataSource =myDs.Tables["T_AVION"];
     
     
     
     
    private void button_affichage_Click(object sender, EventArgs e)
            {
                //on va utiliser la fonction select
                myDa.SelectCommand = new SqlCommand();
                myDa.SelectCommand.Connection = connection;
                myDa.SelectCommand.CommandText = "SELECT * FROM T_AVION";
     
                myDa.Fill(myDs, "T_AVION");
                dataGridView1.DataSource = myDs.Tables["T_AVION"];
     
            }

  3. #3
    Membre averti
    Profil pro
    Développeur informatique
    Inscrit en
    Mai 2009
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2009
    Messages : 25
    Par défaut
    Il te manque le SqlCommandBuilder, avec lui tu ne t'embêtes pas à créer le INSERT il le fera tout seul.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    using( SqlConnection connection = new SqlConnection( stringConnection.ConnectionString ) )
    {
                    SqlDataAdapter adapter = new SqlDataAdapter( "SELECT * FROM T_AVION", connection );
                    SqlCommandBuilder cmd = new SqlCommandBuilder( adapter );
                    DataTable tableAvion = new DataTable();                
                    adapter.Fill(  tableAvion );              
    }
     
    //Remplissage du DataGridView
    dataGridView1.DataSource = tableAvion;
     
    //Mise à jour de la base par rapport au info entrées dans le DataGridView
    adapter.Update( tableAvion );

  4. #4
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 46
    Par défaut
    Merci pour ta reponse mais lorsque je remplis mes textboxs et lorsque je clique sur le bouton il m'affiche juste le contenu de ma datagrivew sans nouvelle donnee


    en fait je m'explique un peu c'est une winform avec 2 textbox qui represent le Modele et le Constructeur de l'avion et un bouton ajouter et afficher pour ajouter les donnees dans la datagrivew j'utilise le mode deconnecté pour faire le mes requets dans la base

    voila l'integralité du 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
    57
    58
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace Test11
    {
        public partial class Form1 : Form
        {
            public SqlConnection connection = new SqlConnection();
            public SqlDataAdapter myDa = new SqlDataAdapter();
            public DataSet myDs = new DataSet();
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                //connection à la base
                connection.ConnectionString = "Data Source=pc-de-sangare\\sqlexpress;Initial Catalog=SaberatourSQL;Integrated Security=True";
     
            }
     
            private void button_ajouter_Click(object sender, EventArgs e)
            {
     
     
                //On reprend tout ce passage
     
                //on va utiliser un Insert
     
               myDa.InsertCommand = new SqlCommand();
     
               myDa.InsertCommand.Connection = connection;
               myDa.InsertCommand.CommandText = "INSERT INTO T_AVION(AV_MODELE, AV_CONSTRUCTEUR) VALUES('" + txt_model + "','" + txt_construc + "')";
     
               dataGridView1.DataSource= myDs.Tables["T_AVION"];
     
            }
     
    private void button_affichage_Click(object sender, EventArgs e)
            {
                //on va utiliser la fonction select
                myDa.SelectCommand = new SqlCommand();
                myDa.SelectCommand.Connection = connection;
                myDa.SelectCommand.CommandText = "SELECT * FROM T_AVION";
     
                myDa.Fill(myDs, "T_AVION");
                dataGridView1.DataSource = myDs.Tables["T_AVION"];
     
            }
    surtout n'hesité pas si vous avez besoin de precision

  5. #5
    Membre averti
    Profil pro
    Développeur informatique
    Inscrit en
    Mai 2009
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2009
    Messages : 25
    Par défaut
    As tu initialisé la propriété DataPropertyName dans les colonnes créées pour le DataGridView? Elle doit avoir le même nom que le nom de ta colonne dans la base, donc AV_MODELE et AV_CONSTRUCTEUR.

  6. #6
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 46
    Par défaut
    je l'ai pas fait et je vois pas vraiment comment le faire c'est ca le gros probleme

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. insertion des donnees dans la base a partie de MS studio 2005
    Par j_esti dans le forum Visual Studio
    Réponses: 6
    Dernier message: 01/09/2008, 12h21
  2. comment editer les donnee d'une base de donnees dans une application xbap?
    Par sanaaafkir dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 19/05/2008, 17h26
  3. Réponses: 2
    Dernier message: 19/10/2007, 21h29
  4. Difficulte d'insertion de valeur dans une base de donnees
    Par blondelle dans le forum C++Builder
    Réponses: 10
    Dernier message: 13/04/2007, 22h19
  5. Réponses: 11
    Dernier message: 01/06/2005, 16h18

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