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++/CLI Discussion :

Problème pour insérer une String dans un champ de texte


Sujet :

C++/CLI

  1. #1
    Candidat au Club
    Profil pro
    Lycéen
    Inscrit en
    Juillet 2010
    Messages
    2
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Juillet 2010
    Messages : 2
    Points : 3
    Points
    3
    Par défaut Problème pour insérer une String dans un champ de texte
    Bonjour/Bonsoir à tous et à toutes,
    je souhaiterais de l'aide afin de créer un programme en C++, j'ai commencé sa création grâce au compilateur Visual C++ Express 2010 (Windows Form), le but de ce programme est de créer une requête SQL à partir d'informations que l'utilisateur met dans deux champs de texte ( richTextBox2 et TextBox1 ) et la requête SQL doit s'afficher dans un autre champ de texte : richTextBox1.

    Form1.h
    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
     
    #pragma once
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
     
    namespace SQL_Gossip {
     
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace std;
     
    	/// <summary>
    	/// Description résumée de Form1
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: ajoutez ici le code du constructeur
    			//
    		}
     
    	protected:
    		/// <summary>
    		/// Nettoyage des ressources utilisées.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::RichTextBox^  richTextBox1;
    	protected: 
    	private: System::Windows::Forms::RichTextBox^  richTextBox2;
    	private: System::Windows::Forms::Button^  button1;
    	private: System::Windows::Forms::Button^  button2;
    	private: System::Windows::Forms::Label^  label1;
    	private: System::Windows::Forms::Label^  label2;
    	private: System::Windows::Forms::LinkLabel^  linkLabel1;
    	private: System::Windows::Forms::TextBox^  textBox1;
    	private: System::Windows::Forms::Label^  label3;
     
     
    	private:
    		/// <summary>
    		/// Variable nécessaire au concepteur.
    		/// </summary>
    		System::ComponentModel::Container ^components;
     
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
    		/// le contenu de cette méthode avec l'éditeur de code.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
    			this->richTextBox2 = (gcnew System::Windows::Forms::RichTextBox());
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->button2 = (gcnew System::Windows::Forms::Button());
    			this->label1 = (gcnew System::Windows::Forms::Label());
    			this->label2 = (gcnew System::Windows::Forms::Label());
    			this->linkLabel1 = (gcnew System::Windows::Forms::LinkLabel());
    			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->label3 = (gcnew System::Windows::Forms::Label());
    			this->SuspendLayout();
    			// 
    			// richTextBox1
    			// 
    			this->richTextBox1->Location = System::Drawing::Point(42, 244);
    			this->richTextBox1->Name = L"richTextBox1";
    			this->richTextBox1->Size = System::Drawing::Size(531, 294);
    			this->richTextBox1->TabIndex = 0;
    			this->richTextBox1->Text = L""; //Pas touche
    			// 
    			// richTextBox2
    			// 
    			this->richTextBox2->Location = System::Drawing::Point(42, 70);
    			this->richTextBox2->Name = L"richTextBox2";
    			this->richTextBox2->Size = System::Drawing::Size(531, 96);
    			this->richTextBox2->TabIndex = 1;
    			this->richTextBox2->Text = L"";
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(440, 172);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(133, 23);
    			this->button1->TabIndex = 2;
    			this->button1->Text = L"Créer la requête SQL.";
    			this->button1->UseVisualStyleBackColor = true;
    			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// button2
    			// 
    			this->button2->Location = System::Drawing::Point(440, 544);
    			this->button2->Name = L"button2";
    			this->button2->Size = System::Drawing::Size(133, 23);
    			this->button2->TabIndex = 3;
    			this->button2->Text = L"Copier la requête.";
    			this->button2->UseVisualStyleBackColor = true;
    			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
    			// 
    			// label1
    			// 
    			this->label1->AutoSize = true;
    			this->label1->Location = System::Drawing::Point(12, 11);
    			this->label1->Name = L"label1";
    			this->label1->Size = System::Drawing::Size(89, 13);
    			this->label1->TabIndex = 4;
    			this->label1->Text = L"ID de la créature.";
    			// 
    			// label2
    			// 
    			this->label2->AutoSize = true;
    			this->label2->Location = System::Drawing::Point(12, 228);
    			this->label2->Name = L"label2";
    			this->label2->Size = System::Drawing::Size(75, 13);
    			this->label2->TabIndex = 5;
    			this->label2->Text = L"Requête SQL.";
    			// 
    			// linkLabel1
    			// 
    			this->linkLabel1->AutoSize = true;
    			this->linkLabel1->Location = System::Drawing::Point(437, 9);
    			this->linkLabel1->Name = L"linkLabel1";
    			this->linkLabel1->Size = System::Drawing::Size(157, 13);
    			this->linkLabel1->TabIndex = 6;
    			this->linkLabel1->TabStop = true;
    			this->linkLabel1->Text = L"Créer pour le serveur : Achéron.";
    			this->linkLabel1->LinkClicked += gcnew System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, &Form1::linkLabel1_LinkClicked);
    			// 
    			// textBox1
    			// 
    			this->textBox1->Location = System::Drawing::Point(12, 27);
    			this->textBox1->Name = L"textBox1";
    			this->textBox1->Size = System::Drawing::Size(100, 20);
    			this->textBox1->TabIndex = 7;
    			this->textBox1->Text = L"";
    			// 
    			// label3
    			// 
    			this->label3->AutoSize = true;
    			this->label3->Location = System::Drawing::Point(15, 54);
    			this->label3->Name = L"label3";
    			this->label3->Size = System::Drawing::Size(84, 13);
    			this->label3->TabIndex = 8;
    			this->label3->Text = L"Texte à afficher.";
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(609, 582);
    			this->Controls->Add(this->label3);
    			this->Controls->Add(this->textBox1);
    			this->Controls->Add(this->linkLabel1);
    			this->Controls->Add(this->label2);
    			this->Controls->Add(this->label1);
    			this->Controls->Add(this->button2);
    			this->Controls->Add(this->button1);
    			this->Controls->Add(this->richTextBox2);
    			this->Controls->Add(this->richTextBox1);
    			this->Name = L"Form1";
    			this->Text = L"SQL Créer un gossip.";
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    			this->ResumeLayout(false);
    			this->PerformLayout();
     
    		}
    #pragma endregion
     
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
    		 {
    			 //
    		 }
    private: System::Void linkLabel1_LinkClicked(System::Object^  sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^  e) 
    		 {
    			 //
    		 }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    		 {
    			richTextBox1->Text = ("UPDATE 'creature_template' SET 'npc_flags'=1 WHERE 'entry'='%s'", textBox1->Text);
    		 }
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
    		 {
    		 }
    };
    }

    Mais lorsque j'entre une valeur dans le champ de texte (textBox1), il s'affiche dans le champs de texte final (richTextBox) :
    "la_valeur_entrée_dans_le_champ_de_texte"
    au lieu de
    "UPDATE 'creature_template' SET 'npc_flags'=1 WHERE 'entry'='la_valeur_entrée_dans_le_champ_de_texte'"
    Donc si j'entre dans mon champ de texte la valeur "64" le champ de texte1 affiche :
    "64"
    au lieu de
    "UPDATE 'creature_template' SET 'npc_flags'=1 WHERE 'entry'='64' "

  2. #2
    r0d
    r0d est déconnecté
    Expert éminent

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    4 264
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Août 2004
    Messages : 4 264
    Points : 6 683
    Points
    6 683
    Billets dans le blog
    2
    Par défaut
    Bonjour,

    Ceci n'est pas du c++ à proprement parler. C'est du c++/cli me semble-t-il.
    Il y a un forum dédié en remontant dans la partie visual c++ du forum; m'est avis que vous obtiendrez plus de réponses, et de meilleure qualité, si vous posez votre question là-bas.

  3. #3
    Expert éminent sénior
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2005
    Messages
    5 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2005
    Messages : 5 154
    Points : 12 266
    Points
    12 266
    Par défaut
    r0d, c'est peut-être du C++/CLI mais le problème n'a rien à voir avec .NET.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    richTextBox1->Text = ("UPDATE 'creature_template' SET 'npc_flags'=1 WHERE 'entry'='%s'", textBox1->Text);
    en C++, la virgule permet de grouper les actions :
    http://msdn.microsoft.com/en-us/libr...xh(VS.80).aspx

    Donc le comportement est normal.
    Ne manque-t-il pas un nom de fonction ou de méthode devant la parenthèse ouvrant ?

    Regardez du coté de string.Format (pas de %s à la C, mais du {0} à la .NET).
    http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

  4. #4
    Candidat au Club
    Profil pro
    Lycéen
    Inscrit en
    Juillet 2010
    Messages
    2
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Juillet 2010
    Messages : 2
    Points : 3
    Points
    3
    Par défaut
    Merci merci de votre réponse rapide. J'ai résolu le problème en remplaçant l'ancienne instruction par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
                            String^ entry 	  = textBox1->Text;
    			String^ gossip_id = textBox2->Text;
    			String^ texte	  = richTextBox2->Text;
     
    			String^ requete = String::Format("UPDATE `creature_template` SET `gossip_menu_id`='{1}', `npcflag`='1' WHERE `entry`='{0}'; \nREPLACE INTO `gossip_menu` (`entry`, `text_id`, `cond_1`, `cond_1_val_1`, `cond_1_val_2`, `cond_2`, `cond_2_val_1`, `cond_2_val_2`) values('{1}','{1}','0','0','0','0','0','0'); \nREPLACE INTO `npc_text` (`ID`, `text0_0`, `text0_1`, `lang0`, `prob0`, `em0_0`, `em0_1`, `em0_2`, `em0_3`, `em0_4`, `em0_5`, `text1_0`, `text1_1`, `lang1`, `prob1`, `em1_0`, `em1_1`, `em1_2`, `em1_3`, `em1_4`, `em1_5`, `text2_0`, `text2_1`, `lang2`, `prob2`, `em2_0`, `em2_1`, `em2_2`, `em2_3`, `em2_4`, `em2_5`, `text3_0`, `text3_1`, `lang3`, `prob3`, `em3_0`, `em3_1`, `em3_2`, `em3_3`, `em3_4`, `em3_5`, `text4_0`, `text4_1`, `lang4`, `prob4`, `em4_0`, `em4_1`, `em4_2`, `em4_3`, `em4_4`, `em4_5`, `text5_0`, `text5_1`, `lang5`, `prob5`, `em5_0`, `em5_1`, `em5_2`, `em5_3`, `em5_4`, `em5_5`, `text6_0`, `text6_1`, `lang6`, `prob6`, `em6_0`, `em6_1`, `em6_2`, `em6_3`, `em6_4`, `em6_5`, `text7_0`, `text7_1`, `lang7`, `prob7`, `em7_0`, `em7_1`, `em7_2`, `em7_3`, `em7_4`, `em7_5`, `WDBVerified`) values('{1}','{2}','{2}','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','','','0','0','0','0','0','0','0','0','1');", gossip_id, entry, texte);
     
    			richTextBox1->Text = requete;
    Merci encore à bientôt.

  5. #5
    Expert éminent sénior
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2005
    Messages
    5 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2005
    Messages : 5 154
    Points : 12 266
    Points
    12 266
    Par défaut
    Attention.

    Chaîne beaucoup trop longue pour être maintenable.
    Trop d'actions faites d'une traite sans vérification des codes retour.
    Manipulation de chaîne de caractères susceptibles de faire n'importe quoi en base voir attaque par "SQL injection" car il n'y a aucun contrôle sur les chaînes.

    N'utiliserez pas la synthèse de requête par concaténation de chaîne mais utilisez des requêtes paramétrées.
    Séparez les différentes étapes dans des méthodes distinctes pour pouvoir avoir une bonne analyse des anomalies en cas de problème et pour traiter correctement le rollback ou les compensations.

    Utilisez des constants strings à la place de chaînes explicites dans le code.
    Ce genre de chaînes et se qui varie le plus durant le développement de l'application et de la base de données.

    En séparent les requêtes par des méthodes distinctes et en utilisant des constantes vous aurez une application qui pourra plus facilement être converti en application multi-tier.

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

Discussions similaires

  1. [XL-2007] Problème pour insérer une ligne avant champ fusionné
    Par Tempas dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 28/04/2010, 14h22
  2. [VBA-E] Problème pour insérer des lignes dans une feuille Excel
    Par skystef dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 16/04/2007, 14h13
  3. Comment fait-on pour insérer une date dans un champs DateTime
    Par gibea00 dans le forum Accès aux données
    Réponses: 1
    Dernier message: 14/01/2007, 01h04
  4. Probléme pour insérer une variable dans un champs
    Par BOUTRAIS dans le forum Access
    Réponses: 2
    Dernier message: 11/04/2006, 22h45
  5. Réponses: 4
    Dernier message: 09/02/2006, 15h20

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