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

ASP.NET Discussion :

[C#] utilisation de dataset et datadapter


Sujet :

ASP.NET

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    801
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 801
    Points : 314
    Points
    314
    Par défaut [C#] utilisation de dataset et datadapter
    Bonjour à tous !!

    J'ai un petit soucide compréhension lorsque j'utilise les objets dataset et datadapter afin d'insérer une ligne dans ma base de données.

    J'ai fait ceci, sui 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
    29
    30
    31
    32
    33
     
     
     
        public string IAmConnected()
        {
            DataSet oDataSet;
            OdbcDataAdapter oDataAdapter;
     
            BD bd = new BD();
            OdbcConnection con = bd.Connect();
            oDataAdapter = new OdbcDataAdapter("select * from connected", con);
            oDataSet = new DataSet("Connected");
            oDataAdapter.Fill(oDataSet, "Connected");
            oDataAdapter.InsertCommand = new OdbcCommand("INSERT INTO connected(LOGIN) Values(?)", con);
            oDataAdapter.InsertCommand.Parameters.Add("@LOGIN", OdbcType.VarChar, 10, "LOGIN");
     
            DataRow oDataRow;
            oDataRow = oDataSet.Tables["Connected"].NewRow();
            oDataRow["LOGIN"] = this.login;
            oDataSet.Tables["Connected"].Rows.Add(oDataRow);
     
            try
            {
                oDataAdapter.Update(oDataSet, "Connected");
            }
            catch (Exception e)
            {
                return e.Message.ToString();
                //Response.Write(e.Message.ToString());
            }
            con.Close();
            return "1";
    }
    J'arrive bien à insérer une valeur dans ma table qui contient un seul champs.
    Mais comment faire pour insérer un enregistrement dans une table qui contient plusieurs champs (2 par exemple ??)

    Merci pour vos réponses !!!

  2. #2
    Membre éprouvé Avatar de guitoux1
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    1 011
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Juin 2006
    Messages : 1 011
    Points : 1 256
    Points
    1 256
    Par défaut
    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
        
    public string IAmConnected()
    {
            DataSet oDataSet;
            OdbcDataAdapter oDataAdapter;
    
            BD bd = new BD();
            OdbcConnection con = bd.Connect();
            oDataAdapter = new OdbcDataAdapter("select * from connected", con);
            oDataSet = new DataSet("Connected");
            oDataAdapter.Fill(oDataSet, "Connected");
            oDataAdapter.InsertCommand = new OdbcCommand("INSERT INTO connected (LOGIN, PASSWORD) Values (?, ?)", con);
            oDataAdapter.InsertCommand.Parameters.Add("@LOGIN", OdbcType.VarChar, 10, "LOGIN");
            oDataAdapter.InsertCommand.Parameters.Add("@PASSWORD", OdbcType.VarChar, 10, "PASSWORD");
    
            DataRow oDataRow;
            oDataRow = oDataSet.Tables["Connected"].NewRow();
            oDataRow["LOGIN"] = this.login;
            oDataRow["PASSWORD"] = this.password;
            oDataSet.Tables["Connected"].Rows.Add(oDataRow);
    
            try
            {
                oDataAdapter.Update(oDataSet, "Connected");
            }
            catch (Exception e)
            {
                return e.Message.ToString();
                //Response.Write(e.Message.ToString());
            }
            con.Close();
            return "1";
    }

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    801
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 801
    Points : 314
    Points
    314
    Par défaut
    Ah d'accord !
    Merci Guitoux.
    C'est la row de datarow qui m'a embrouillé...
    Je pensais qu'il fallait que je mette mon enregistrement entier dans le datarow["login"]. Mais en fait, datarow["login"] représente "une cellule", un champs d'une ligne.

    Merci Guitoux !!

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

Discussions similaires

  1. Peut-on utiliser les DataSet avec Oracle ?
    Par etoile_de_vie dans le forum C#
    Réponses: 19
    Dernier message: 28/04/2008, 16h13
  2. utiliser le dataset d'un web service
    Par k750_b dans le forum Services Web
    Réponses: 1
    Dernier message: 04/03/2008, 15h11
  3. Utilisation du dataset fortement typé
    Par s.n.a.f.u dans le forum Général Dotnet
    Réponses: 2
    Dernier message: 06/04/2007, 19h39
  4. [C#][Mysql]Comment utiliser plusieurs dataset sur la même connexion
    Par Invité dans le forum Accès aux données
    Réponses: 3
    Dernier message: 05/12/2006, 09h30
  5. [C#]Comment utiliser un dataset dans plusieurs...
    Par Sonny dans le forum Windows Forms
    Réponses: 5
    Dernier message: 21/11/2005, 14h03

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