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 :

[C#][MySQL]Re - Synchro DataGridView/dataBase


Sujet :

Windows Forms

  1. #1
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut [C#][MySQL]Re - Synchro DataGridView/dataBase
    Bonjour, si je mets "Re -" c'est pour faire suite à ce poste.

    J'ai bien essayé de mettre en application l'autre post, mais rien à faire, il ne veux pas fonctionner. Pour les méthodes Update et Delete, j'ai des exceptions "DBConcurrency" et pour la méthode Insert, il m'insert un champ null

    le remplissage du dataGridView s'effectue selon ce code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
          _MySQLConnection.ConnectionString = this._ConnectionString;
     
    			_MySQLDataAdapter.SelectCommand = new MySqlCommand("SELECT * FROM test", _MySQLConnection);
    			_MySQLDataAdapter.Fill(dataSet1, "test");
    			this.bindingSource1.DataSource = this.dataSet1;
    			this.bindingSource1.DataMember = "test";
    			this.dataGridView1.DataSource = this.bindingSource1;
    Voici la partie situé dans un bouton, afin d'effectuer la mise à jour :
    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
     
    // paramétrage commande DELETE
    			_MySQLDataAdapter.DeleteCommand = new MySql.Data.MySqlClient.MySqlCommand("DELETE FROM test WHERE test.id = @id", _MySQLConnection); 
    			MySql.Data.MySqlClient.MySqlParameter _MySQLDeleteParameter = _MySQLDataAdapter.DeleteCommand.Parameters.Add("@id", MySql.Data.MySqlClient.MySqlDbType.UInt32);
    			_MySQLDeleteParameter.SourceColumn = "id";
    			_MySQLDeleteParameter.SourceVersion = DataRowVersion.Original;
     
    			// paramétrage commande INSERT
    			_MySQLDataAdapter.InsertCommand = new MySqlCommand("INSERT INTO test (nom) VALUES (@nom)", _MySQLConnection);
    			_MySQLDataAdapter.InsertCommand.Parameters.Add("@nom", MySql.Data.MySqlClient.MySqlDbType.VarString, 45, "nom");
    			MySqlParameter _MySQLInsertParameter = _MySQLDataAdapter.InsertCommand.Parameters.Add("@id", MySql.Data.MySqlClient.MySqlDbType.UInt32);
    			_MySQLInsertParameter.SourceColumn = "id";
    			_MySQLInsertParameter.SourceVersion = DataRowVersion.Original;
     
    			// paramétrage commande UPDATE
    			_MySQLDataAdapter.UpdateCommand = new MySqlCommand("UPDATE test SET test.nom = @nom WHERE test.id = @id", _MySQLConnection);
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("@nom", MySqlDbType.VarString, 45, "nom");
    			MySqlParameter _MySQLUpdateParameter = _MySQLDataAdapter.UpdateCommand.Parameters.Add("@id", MySql.Data.MySqlClient.MySqlDbType.UInt32);
    			_MySQLUpdateParameter.SourceColumn = "id";
    			_MySQLUpdateParameter.SourceVersion = DataRowVersion.Original;
     
    			// mise-à-jour
    			_MySQLDataAdapter.Update(this.dataSet1, "test");
    J'ai essayé de suivre l'exemple donné par Neguib au mieux, mais je pense avoir oublier un truc.

  2. #2
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    un p'ti UP car je n'ai toujours pas trouvé réponse à mon problème.

  3. #3
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    Bon, c'est zarb quand même d'être le seul à utiliser C# et MySQL et de vouloir faire ce genre de truc ....
    J'ai avancé un chouia, mais toujours pas toruvé de solution, je me suis occupé de la partie delete :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
             // paramétrage commande DELETE
             _MySQLDataAdapter.DeleteCommand = new MySql.Data.MySqlClient.MySqlCommand("DELETE FROM test WHERE test.id = @id", _MySQLConnection);
             MySql.Data.MySqlClient.MySqlParameter _MySQLDeleteParameter = _MySQLDataAdapter.DeleteCommand.Parameters.Add("@id", MySql.Data.MySqlClient.MySqlDbType.UInt32);
             _MySQLDeleteParameter.SourceColumn = "id";
             _MySQLDeleteParameter.SourceVersion = DataRowVersion.Original;
    si je modifie le paramètre "@id" par "?id", j'ai déjà une exception différente :
    "@id" : Violation de l'accès concurrentiel : DeleteCommand a affecté 0 des enregistrements 1 attendus.
    "?id" : Only byte arrays and strings can be serialized by MySqlBinary

    BOn, il faudrait que je jete un oeil sur l'utilisation de '@' ou '?' en tête des paramètres, vu que l'erreur n'est pas la même, c'est que ça a son importance.

  4. #4
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    en allant ci et là via google, j'ai trouvé que MySQL avait changé '@' par '?', et tout le monde voit '?' comme ZE solution, mais chez moi, il y a un hic, alors trouvons le.

  5. #5
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    j'ai remplacé la commande INSERT par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
            // paramétrage commande INSERT
    			_MySQLDataAdapter.InsertCommand = new MySqlCommand("INSERT INTO test (nom) VALUES (?nom)", _MySQLConnection);
    			_MySQLDataAdapter.InsertCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45,"nom");
    et ça fonctionne.

  6. #6
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    Bon, ce qui foire dans les commande UPDATE et DELETE c'est le paramètre "?id". Supposons que je veuille modifier la ligne dont id = 5, si je mets :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    _MySQLDataAdapter.UpdateCommand = new MySqlCommand("UPDATE test SET test.nom = ?nom WHERE test.id = ?id", _MySQLConnection);
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45, "nom");
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?id", MySqlDbType.UInt32, 0, "id");
    ça ne fonctionne pas, mais si je test avec :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    _MySQLDataAdapter.UpdateCommand = new MySqlCommand("UPDATE test SET test.nom = ?nom WHERE test.id = '5'", _MySQLConnection);
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45, "nom");
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?id", MySqlDbType.UInt32, 0, "id");
    là ça fonctionne.

  7. #7
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    Bon, ce qui foire dans les commande UPDATE et DELETE c'est le paramètre "?id". Supposons que je veuille modifier la ligne dont id = 5, si je mets :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    _MySQLDataAdapter.UpdateCommand = new MySqlCommand("UPDATE test SET test.nom = ?nom WHERE test.id = ?id", _MySQLConnection);
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45, "nom");
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?id", MySqlDbType.UInt32, 0, "id");
    ça ne fonctionne pas et j'ai toujours l'erreur
    Only byte arrays and strings can be serialized by MySqlBinary
    Mais si je test avec :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    _MySQLDataAdapter.UpdateCommand = new MySqlCommand("UPDATE test SET test.nom = ?nom WHERE test.id = '5'", _MySQLConnection);
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45, "nom");
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?id", MySqlDbType.UInt32, 0, "id");
    là ça fonctionne.

  8. #8
    Membre éprouvé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 98
    Par défaut
    Hé bé ayé c'est bon, en fait l'astuce vient de mettre le paramètre "?id" en String et non pas UInt32.
    A titre d'info pour ceux que ça intéresse, 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
     
    private void Form2_Load(object sender, EventArgs e)
    		{
    			_MySQLConnection.ConnectionString = this._ConnectionString;
     
    			_MySQLDataAdapter.SelectCommand = new MySqlCommand("SELECT * FROM test", _MySQLConnection);
    			_MySQLDataAdapter.Fill(dataSet1, "test");
    			this.dataGridView1.DataSource = this.dataSet1.Tables[0];
            }
     
    		private void button1_Click(object sender, EventArgs e)
    		{
    			// paramétrage commande DELETE
    			_MySQLDataAdapter.DeleteCommand = new MySql.Data.MySqlClient.MySqlCommand("DELETE FROM test WHERE test.id = ?id", _MySQLConnection);
    			_MySQLDataAdapter.DeleteCommand.Parameters.Add("?id", MySql.Data.MySqlClient.MySqlDbType.String, 0, "id");
    			_MySQLDataAdapter.DeleteCommand.Parameters["?id"].SourceVersion = DataRowVersion.Original;
     
    			// paramétrage commande INSERT
    			_MySQLDataAdapter.InsertCommand = new MySqlCommand("INSERT INTO test (nom) VALUES (?nom)", _MySQLConnection);
    			_MySQLDataAdapter.InsertCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45,"nom");
     
    			// paramétrage commande UPDATE
    			_MySQLDataAdapter.UpdateCommand = new MySqlCommand("UPDATE test SET test.nom = ?nom WHERE test.id = ?id", _MySQLConnection);
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?nom", MySqlDbType.VarString, 45, "nom");
    			_MySQLDataAdapter.UpdateCommand.Parameters.Add("?id", MySqlDbType.String, 0, "id");
     
    			_MySQLConnection.Open();
    			// mise-à-jour
    			_MySQLDataAdapter.Update(this.dataSet1, "test");
    			_MySQLConnection.Close();
    		}
    ++

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 03/06/2013, 16h55
  2. Réponses: 17
    Dernier message: 08/08/2006, 14h08
  3. Problème Java/MySql : "Unknown database"
    Par darkflo dans le forum JDBC
    Réponses: 3
    Dernier message: 24/03/2006, 11h34
  4. Réponses: 20
    Dernier message: 13/01/2006, 20h42
  5. Extraire la date la plus récente Database MYsql
    Par brazza dans le forum Requêtes
    Réponses: 2
    Dernier message: 21/11/2004, 02h34

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