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 :

[c# - mysql ]command INSERT


Sujet :

C#

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut [c# - mysql ]command INSERT
    Bonjour,

    je souhaite développer 1 application utilisant mysql et c#. J'ai réussis à me connecter. Il me faut maintenant envoyé 2 textfield dans ma base de donnée.

    J'ai écris une partie 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
                public Boolean insertDonne() 
                { 
                    string sName = txtName.Text; 
                    string sFirstName = txtFirstName.Text; 
                    SqlCommand insertDonnee = new SqlCommand("INSERT INTO contact VALUES('', name, firstName);"); 
     
                    try 
                    { 
                        //Ici le code qui insère les données à la base mysql EasyPHP
                        toolStripStatusLabel1.Text = "Données ajoutées avec succès."; 
     
                        return true; 
                    } 
                    catch (MySqlException myEx) 
                    { 
                        toolStripStatusLabel1.Text = "Error " + myEx.GetType() + " : d'exécuter la requête."; 
                        return false; 
                    } 
                }
    Mes questions:

    - Ou placer ma fonction? dans le bouton dans la form? Si dans la form, comment appeller la fonction dans le bouton?

    - Ma command d'insertion est-elle complète? Si non, que me faut-il ajouté et ou?

    Je vous remercie de l'attention porté à mon message et vous souhaite une excellente journée.

    Meilleures salutation,

    Yannick

  2. #2
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Points : 4 061
    Points
    4 061
    Par défaut
    Ou placer ma fonction? dans le bouton dans la form? Si dans la form, comment appeller la fonction dans le bouton?
    Si tu ne sais pas comment appeler une fonction en .Net, je te conseil de revoir les bases de la programmation en .Net, car sinon tu vas te galérer pour ce que tu fais.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 141
    Points : 110
    Points
    110
    Par défaut
    pour ta fonction, il faut générer l'évènement Click du bouton.
    Dans la fonction nouvellement créée, tu appelle ta fonction.

    Mais ced600 à raison, si tu ne sais pas faire un évènement je te conseil de revoir les bases du c#. Pour t'aider sur les évènements : FAQ c# http://freddyboy.developpez.com/dotn...es/events/#LIV

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    Jme suis mal exprimé, désolé.

    Je cherche la fonction qui me permet d'insérer correctement mes données dans ma base MySQL

    Edit: J'ai trouvé le 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
    public Boolean insertDonnee()
            {
                string sName = txtName.Text;
                string sFirstName = txtFirstName.Text;
                SqlCommand insertDonnee = new SqlCommand("INSERT INTO contact VALUES('', name, firstName);");
     
     
                try
                {
     
                    insertDonnee.Connection.Open();
                    insertDonnee.ExecuteNonQuery();
                    toolStripStatusLabel1.Text = "Données ajoutées avec succès.";
                    insertDonnee.Connection.Close();
     
                    return true;
                }
                catch (MySqlException myEx)
                {
                    toolStripStatusLabel1.Text = "Error " + myEx.GetType() + " : Impossible d'exécuter la requête.";
                    return false;
                } 
            }
    l'erreur: La référence d'objet n'est pas défini à une instance d'objet.

    Solution?

  5. #5
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Citation Envoyé par kiwi645 Voir le message
    Jme suis mal exprimé, désolé.

    Je cherche la fonction qui me permet d'insérer correctement mes données dans ma base MySQL
    Toujours pas très clair.
    Tu as ta fonction pour ton INSERT oui ?
    Elle fonctionne ?

    Si oui tu l'appelles sur l'évènement Click d'un bouton par exemple.

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    Scuse on a répondu en meme temps, vois mon poste ci-dessus, je l'ai éditer.

  7. #7
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Il manque la création de la SqlConnection.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    SqlConnection connection = new SqlConnection("...");
    connection.Open();
    ...
    insertDonnee.Connection = connection
    ...

  8. #8
    Expert confirmé
    Avatar de debug
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    1 034
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 034
    Points : 4 093
    Points
    4 093
    Par défaut
    La ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SqlCommand insertDonnee = new SqlCommand("INSERT INTO contact VALUES('', name, firstName);");
    te permet de définir la requete à exécuter.

    Il te faut ensuite donner à la commande l'ordre de s'éxecuter par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    insertDonnee.ExecutenonQuery();

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    c'est dans une autre fonction :} La connection fonctionne:

    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
            public Boolean SqlConnection()
            {
                string DBName = "courtage";
                string Server = "localhost";
                string Login = "root";
                string Password = "";
     
                string connectionString = "Server=" + Server +
                    ";Database=" + DBName +
                    ";User ID=" + Login +
                    ";Password=" + Password + ";";
                try
                {
                    ConnectionRessource = new MySqlConnection(connectionString);
                    toolStripStatusLabel1.Text = "Ouverture de la connection à la table" + DBName + ".";
                    ConnectionRessource.Open();
                    toolStripStatusLabel1.Text = "connection établie avec succès à la table " + DBName + ".";
     
                    return true;
                }
                catch (MySqlException myEx)
                {
                    toolStripStatusLabel1.Text = "Error " + myEx.GetType() + " : Impossible de se connecter à la base de donnée.";
     
                    return false;
                }
     
            }
    :]

  10. #10
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Oui mais quand tu crée ton SqlCommand tu ne lui affectes pas ta SqlConnection.

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    Je l'ai inséré dans mon try.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
                try
                {
                    insertDonnee.Connection.Open();
                    insertDonnee.ExecuteNonQuery();
                    toolStripStatusLabel1.Text = "Données ajoutées avec succès.";
                    insertDonnee.Connection.Close();
     
                    return true;
                }
                catch (MySqlException myEx)
                {
                    toolStripStatusLabel1.Text = "Error " + myEx.GetType() + " : Impossible d'exécuter la requête.";
                    return false;
                }
    Mais il me donne l'erreur cité cidessus au .open();

  12. #12
    Expert confirmé
    Avatar de debug
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    1 034
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 034
    Points : 4 093
    Points
    4 093
    Par défaut
    Il faut que tu ouvres directement ton objet ConnectionRessource

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    Comment ça?

  14. #14
    Expert confirmé
    Avatar de debug
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    1 034
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 034
    Points : 4 093
    Points
    4 093
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ConnectionRessource.Open();

  15. #15
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    erf j'avais deja essayé ceci:

    The connection is already open.

  16. #16
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 141
    Points : 110
    Points
    110
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    SqlCommand insertDonnee = new SqlCommand("INSERT INTO contact VALUES('', name, firstName);", objetConnection);
    utilise le constructeur ci-dessus! Sinon comment veux-tu que ton MySqlCommand sache quelle connection utilisé?

  17. #17
    Inactif  
    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Janvier 2007
    Messages
    6 604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet NTIC

    Informations forums :
    Inscription : Janvier 2007
    Messages : 6 604
    Points : 13 314
    Points
    13 314
    Par défaut
    SqlCommand n'est valide que pour des connexions avec Sql Server/Sql Express, pas pour MySql.

    Il faut utiliser DbCommand. (ou MySqlDbCommand ?)

  18. #18
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 141
    Points : 110
    Points
    110
    Par défaut
    bien vue bluedeep en effet, pour MySql la classe à utiliser est MySqlCommand

  19. #19
    En attente de confirmation mail
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    98
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 98
    Points : 64
    Points
    64
    Par défaut
    connectionRessource.Open();
    Ensuit il faut mettre un
    insertdonnee.connection = connectionRessource;

    En gros tu mets :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    string connection ="Server=........";
    MySqlConnection connectionRessource = new MySqlConnection(connection);
    connectionRessource.Open();
    MySqlCommanf insertDonnee = new MySqlCommand();
    insertDonnee.connection = connectionRessource;
    insertDonnee.CommandText= "INSERT......";
    MySqlDataReader executer = insertDonnee.ExecuteReader();
    executer.Close();
    connectionRessource.Close();
    Enfin moi j'ai appris comme ca et ca marche niquel

  20. #20
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2007
    Messages : 93
    Points : 56
    Points
    56
    Par défaut
    Le soucis persiste, voici l'erreur:

    http://img168.imageshack.us/my.php?image=insertnw6.png

    et voici le 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
    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
    106
    107
    108
    109
    110
     
    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;
    using MySql.Data.MySqlClient;
     
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            private MySqlConnection ConnectionRessource;
            private MySqlCommand clientSelect;
            private MySqlCommand insertDonnees;
     
            public Form1()
            {
                InitializeComponent();
                SqlConnection();
            }
            public Boolean SqlConnection()
            {
                string DBName = "courtage";
                string Server = "localhost";
                string Login = "root";
                string Password = "";
     
                string connectionString = "Server=" + Server +
                    ";Database=" + DBName +
                    ";User ID=" + Login +
                    ";Password=" + Password + ";";
                try
                {
                    ConnectionRessource = new MySqlConnection(connectionString);
                    toolStripStatusLabel1.Text = "Ouverture de la connection à la table" + DBName + ".";
                    ConnectionRessource.Open();
                    toolStripStatusLabel1.Text = "connection établie avec succès à la table " + DBName + ".";
     
                    return true;
                }
                catch (MySqlException myEx)
                {
                    toolStripStatusLabel1.Text = "Error " + myEx.GetType() + " : Impossible de se connecter à la base de donnée.";
     
                    return false;
                }
     
            }
            public Boolean selectClient()
            {
                //Création de la commande 
                string selectCommand = "SELECT * FROM contact;";
                clientSelect = new MySqlCommand(selectCommand); //Commande
     
                try //Procédure de test 
                {
                    //toolStripStatusLabel2.Text = "Liste des contacts chargé avec succès"; //Informations a l'utilisateur du succès 
     
                    return true;
                }
                catch (MySqlException myEx) //Erreur 
                {
                    toolStripStatusLabel2.Text = "Error " + myEx.GetType() + " : Impossible de lister les contacts"; //Information à l'utilisateur de l'erreur 
     
                    return false;
                }
            }
     
            public Boolean insertDonnee()
            {
                string sName = txtName.Text;
                string sFirstName = txtFirstName.Text;
     
                MySqlCommand insertDonnees = new MySqlCommand();
                insertDonnees.CommandText = "INSERT INTO contact(id, name, firstName) VALUES('', " + sName + ", " + sFirstName + ");";
     
                try
                {
                    insertDonnees.Connection = ConnectionRessource;
                    MySqlDataReader insertExecuter = insertDonnees.ExecuteReader();
                    insertExecuter.Close();
                    toolStripStatusLabel1.Text = "Donnée ajouté avec succès à la table.";
     
                    return true;
     
                }
                catch (MySqlException myEx)
                {
                    MessageBox.Show("Error: " + myEx + "Impossible d'ajouté les données");
     
                    return false;
                }
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                selectClient();
            }
     
            private void btnInsert_Click(object sender, EventArgs e)
            {
                insertDonnee();
            }
        }
    }
    Je vous remercie encore de l'aide que vous m'apportez.

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. [MySQL] Erreur commande "INSERT INTO"
    Par dieu08 dans le forum JDBC
    Réponses: 3
    Dernier message: 22/10/2012, 16h15
  2. [MySQL] Deux insert into?
    Par GYK dans le forum Langage SQL
    Réponses: 11
    Dernier message: 12/06/2006, 11h33
  3. form6i la commande insert->?
    Par seif_eddine dans le forum Forms
    Réponses: 5
    Dernier message: 07/06/2006, 00h03
  4. Delphi - Mysql 5.0 et commande insert
    Par zorville dans le forum Requêtes
    Réponses: 4
    Dernier message: 23/04/2006, 17h59
  5. [VB.NET][MySQL]ID insertion dans une base MySQL par ODBC
    Par leSeb dans le forum Windows Forms
    Réponses: 2
    Dernier message: 21/03/2006, 10h58

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