Ado.net Dataset Update, Insert, delete (Problème de mise à jour dans la BD)

loheslath Messages postés 1 Date d'inscription mercredi 20 février 2008 Statut Membre Dernière intervention 4 janvier 2009 - 4 janv. 2009 à 21:33
NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 - 5 janv. 2009 à 22:24
Je voudrais utiliser Ado.net Dataset pour insérer, mettre à jour et supprimer des données dans une base de données SqlServer.

L'insertion marche bien, mais le mise à jour et la suppression ne marche pas (voir code ci-dessous). En effet quand je fais la suppression de l'enregistrement il est bien supprimé dans le dataset (j'ai vérifié par déboguage) mais ma requête DELETE n'est pas répercutée dans la base de données après l'appel de la méthode DataAdapter.Update(...). Il en est de même pour la mise à jour. Or avec les mêmes méthodes utilisées l'insertion marche bien.
LES CODES DES TROIS OPERATIONS :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
 
namespace Ado_DATASET_Mieux
{
    class Program
    {
        static void Main(string[] args)
        {
            string strConnexion = "Data Source=MAWOULE\\SQLEXPRESS; Integrated Security=True;" + "Initial Catalog=Northwind";

            try
            {
            SqlConnection oConnection = new SqlConnection(strConnexion);
            oConnection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();

             // // // // // // // // // // // // // // // //
            // Create the InsertCommand  //
            // // // // // // // // // // // // // // // //
               command = new SqlCommand(
                "INSERT INTO Categories(CategoryName, Description, Picture) " +
                "Values(@CategoryName,@Description,@Picture)", oConnection);

            // Add the parameters for the InsertCommand.
            command.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
            command.Parameters.Add("@Description", SqlDbType.NText, 16, "Description");
            command.Parameters.Add("@Picture", SqlDbType.Image, 16, "Picture");

            adapter.InsertCommand = command;

            DataRow oDataRow;
            byte[] byteArray = { 0x00, 0x00 };

            oDataRow = oDataSet.Tables["Categories"].NewRow();
            oDataRow["CategoryName"] = "Wine2";
            oDataRow["Description"] = "French Wine";
            oDataRow["Picture"] = byteArray;

            oDataSet.Tables["Categories"].Rows.Add(oDataRow);

            // Mise à jour de la source de données à partir du DataSet
            adapter.Update(oDataSet, "Categories");

            // Rechargement des données de la source mise à jour
            oDataSet.Clear();
            adapter.Fill(oDataSet, "Categories");

            // Affichage du contenu de oDataSet après insertion de données
            Console.WriteLine(" *** Liste des catégories après l'insertion *** ");
            for (int i = 0; i < oDataSet.Tables["Categories"].Rows.Count; i++)
            {
                Console.WriteLine("\t{0}\t{1}", oDataSet.Tables["Categories"].Rows[i][0].ToString(), oDataSet.Tables["Categories"].Rows[i][1].ToString());
            }
            Console.WriteLine("\n");

             // // // // // // // // // // // // // // // // //
            // Create the UpdateCommand  //
            // // // // // // // // // // // // // // // // //
 
             command = new SqlCommand(
                             "UPDATE Categories SET  CategoryName = @CategoryName " +
                             "WHERE CategoryID = '10'", oConnection);
  
             // Add the parameters for the SelectCommand.
             command.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
 
             adapter.UpdateCommand = command;

             DataRow[] CategoriesRow =  oDataSet.Tables["Categories"].Select("CategoryID = 14 ");

             CategoriesRow[0]["CategoryName"] = "tata";
 
             CategoriesRow[0].AcceptChanges();
             
             // Mise à jour de la source de données à partir du DataSet
              adapter.Update(oDataSet, "Categories");
           
             // Rechargement des données de la source mise à jour
             oDataSet.Clear();
             adapter.Fill(oDataSet, "Categories");

             // Affichage du contenu de oDataSet après mise à jour de données
             Console.WriteLine(" *** Liste des catégories après la mise à jour *** ");
             for (int i = 0; i < oDataSet.Tables["Categories"].Rows.Count; i++)
             {
                 Console.WriteLine("\t{0}\t{1}", oDataSet.Tables["Categories"].Rows[i][0].ToString(),  oDataSet.Tables["Categories"].Rows[i][1].ToString());
             }
             Console.WriteLine("\n");
             Console.ReadLine();
 

            // // // // // // // // // // // // // // // //
           // Create the DeleteCommand  //
           // // // // // // // // // // // // // // // //

             command = new SqlCommand(
                 "DELETE FROM Categories WHERE CategoryName = @CategoryName", oConnection);

             // Add the parameters for the DeleteCommand.
             command.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
 
             DataRow [] oDataRows;             oDataRows oDataSet.Tables["Categories"].Select("CategoryName 'tata'");
             oDataRows[0].Delete();

             oDataSet.AcceptChanges();
            adapter.DeleteCommand = command;

            oDataSet.Clear();
             adapter.Fill(oDataSet, "Categories");
   
            // Affichage du contenu de oDataSet après Suppression de données
            Console.WriteLine(" *** Liste des catégories après Suppression *** ");
             for (int i = 0; i < oDataSet.Tables["Categories"].Rows.Count; i++)
             {
                 Console.WriteLine("\t{0}\t{1}", oDataSet.Tables["Categories"].Rows[i][0].ToString(),                    oDataSet.Tables["Categories"].Rows[i][1].ToString());
             }
           

            oConnection.Close();
            Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("L'erreur suivante a été rencontrée :" + e.Message);
                Console.ReadLine();
            }
        }
    }
}   

1 réponse

NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
5 janv. 2009 à 22:24
Bonjour,

Ton code est en C# apparemment, je te conseil donc d'aller sur www.csharpfr.com pour poser ta question.

http://nhen0039.chez-alice.fr/index.php
0
Rejoignez-nous