Vider/supprimer une table de SqlParameter

Résolu
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011 - 15 avril 2011 à 15:48
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011 - 19 avril 2011 à 08:54
Bonjour !

JE suis confronté a un petit problème : Je doit remettre a zéro les paramètres de ma classe comme ceci :
public void RAZ()
        {
            this.Cible = "";
            this.CSecteur = "";
            this.EtatVal = 0;
            this.From = "";
            this.GroupBy = "";
            this.Into = "";
            this.IntoDbf = "";
            this.NbLigne = 0;
            this.NomConnection = 0; // a transformer en pointeur        
            this.OrderBy = "";
            this.ResConnect = 0;
            this.Select = "";
            this.SourceDbf = "";
            this.Type = "";
            this.Values = "";
            this.Where = "";            
            this.Tabstring.RemoveRange(0, nb_string);
            this.nb_string = 0;
            // ICI remettre a 0 TabParam
            this.nb_param = 0;
           
        }


TabParam est défini comme ceci en tant que variable globale.

SqlParameter[] TabParam= new SqlParameter[50];



je voudrait pouvoir "vider" ma table pour quelle soit comme a l'origine mais je n'ai pas trouvé un moyen de la détruire puis de la re-déclarer globale
(déja la détruire je ne sais pas )

Si quelqu'un peut m'aider

merci d'avance !

25 réponses

LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
15 avril 2011 à 16:02
Salut,

un simple
TabParam= new SqlParameter[50];
de vrait faire l'affaire non ?

et
this.Tabstring.Clear()
ne va pas ?? c'est quoi comme objet ?
3
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
15 avril 2011 à 16:49
Non, je confirme...

1) tu peux remplacer
this.Tabstring.RemoveRange(0, nb_string);
par
this.Tabstring.Clear();

2) Pour réinitialiser ta variable "TabParam" tu peux écrire :
TabParam= new SqlParameter[50];

L'erreur que tu obtiens "La requête paramétré 'blabla' attend le paramètre @Parameter1, qui n'a pas été fourni" indique que tu n'as pas réaffecter les paramètres utilisés par ta requête...

Assures toi d'avoir bien écrit TabParam[ 0 ] = new SqlParameter(...);
avant l'exécution de ta requête !
3
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
15 avril 2011 à 19:14
Je te propose le code suivant, il permet d'exécuter la requête après avoir extrait les SqlParameter ainsi que les valeurs des variables associées :

private void ExecuteQuery( string query )
{
    // Remplace les caractères '?' par le caractère '@'
    query = query.Replace( '?', '@' );

    // Affecte la requête à l'objet SqlCommand
    command.CommandText = query;

    // Récupère le nom des variables
    ArrayList variableNames = ExtractVariableNames( query );

    // Affecte la liste de SqlParameters à l'objet SqlCommand
    command.Parameters.Clear();
    command.Parameters.AddRange( ExtractSqlParameters( variableNames ) );

    // Exécute la requête
    command.CommandText = query;
    SqlDataReader reader = command.ExecuteReader();
}

// Extrait le nom des variables contenu dans la requête
private ArrayList ExtractVariableNames( string test )
{
    // Initialise le tableau de noms de variables
    ArrayList variableNames = new ArrayList();

    // Parcourt la requête à exécuter
    for( int charIndex = 0; charIndex < test.Length; charIndex++ )
    {
        if( test[ charIndex ] == '@' )
        {
            // Début de nom de variable
            int startIndex = charIndex + 1;

            // Recherche la fin du nom de variable
            charIndex++;
            while( charIndex < test.Length && test[ charIndex ] != ',' && test[ charIndex ] != ' ' && test[ charIndex ] != ')' )
                charIndex++;

            // Ajoute le nom de la variable dans le tableau
            string variableName = test.Substring( startIndex, charIndex - startIndex );
            if( !variableNames.Contains( variableName ) )
                variableNames.Add( variableName );
        }
    }

    // Retourne le tableau
    return variableNames;
}

// Extrait les valeurs des variables passées en paramètre et retourne une liste des SqlParamaters associés
private SqlParameter[] ExtractSqlParameters( ArrayList variableNames )
{
    // Initialise le tableau de SqlParameters
    SqlParameter[] sqlParameters = new SqlParameter[ variableNames.Count ];

    // Parcourt les noms de variables
    for( int varIndex = 0; varIndex < variableNames.Count; varIndex++ )
    {
        // Extrait le SqlParameter associé
        sqlParameters[ varIndex ] = GetParameter( variableNames[ varIndex ].ToString() );
    }

    // Retourne le tableau 
    return sqlParameters;
}

// Retourne un SqlParameter affecté de la valeur d'une variable d'après son nom
private SqlParameter GetParameter( string varName )
{
    FieldInfo field = typeof( Program ).GetField( varName, BindingFlags.NonPublic | BindingFlags.Static );

    if( field == null )
        throw new ArgumentException( "La variable '" + varName + "' n'est pas définie dans la classe Program" );

    SqlDbType dbType;
    switch( field.FieldType.FullName )
    {
        case ("System.String"):
            dbType = SqlDbType.VarChar;
            break;

        case ("System.Int32"):
            dbType = SqlDbType.Int;
            break;

        case ("System.Double"):
            dbType = SqlDbType.Float;
            break;

        case ("System.DateTime"):
            dbType = SqlDbType.DateTime;
            break;

        default:
            throw new ArgumentException( "Une variable de type '" + field.FieldType.FullName + "' ne peut pas être converti en SqlDbType" );
    }

    SqlParameter parameter = new SqlParameter( varName, dbType );
    parameter.Value = field.GetValue( null );

    return parameter;
}
3
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
18 avril 2011 à 06:20
Re...

J'ai trouvé d'où venait le fait que la seconde requête plante :
Tu n'as pas vider la liste "TableDefini" dans ton RAZ; du coup dans la méthode
Execution, il positionne le booléen "trouve" à true est ajoute un paramètre vide...

Il faut donc ajouter TableDefini.Clear(); dans ta méthode RAZ()


Autre chose, toujours dans la méthode Execution(), il faut vider la liste
de paramètres de l'objet SqlCommand juste avant ou après l'appel à
parser( XExec );

donc : command.Parameters.Clear();


Je pense que ça suffira, mais je continu les investigations ;)
3

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
18 avril 2011 à 10:34
Concernant l'allègement...

1) Comme je te l'avais proposé dans un des post précédents, remplacer les '?' par les '@' en faisant : toto = toto.Replace( '?', '@' );

2) Remplacer ta méthode private bool existe( string varName ) par TableDefini.Contains( varName );

3) Idem pour public bool existestring( string varName ) à remplacer par Tabstring.Contains( varName );

4) Je pense d'ailleur que tu peux te débarrasser des tableaux TabString, TabParam et TableDefini... Il n'y a aucun mal à ré-instancier tes SqlParameter à chaque requête !

N'hésites pas si tu as d'autres question.
En attendant, bonne continuation !!

;)
3
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
19 avril 2011 à 05:55
Salut,

J'ai vu deux erreurs dans "btn_select_Click" !
1) Tu n'insères pas " AND " entre les deux paramètres
2) Tu affectes textBox1 au deux champs Etendu et Action (je pense que pour Action c'est textBox2)

Si cela ne change rien envois moi ton projet par mail : ludinski91@Gmail.com
Ce sera plus facile de t'aider comme ça...

A+
3
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
19 avril 2011 à 08:45
J'ai trouvé... je t'ai envoyé un mail en retour !
Tu effaces trop vite la liste de SqlParameter de l'objet SqlCommand
qui est utilisé juste après par SqlDataAdapter...
3
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
15 avril 2011 à 16:14
ben visiblement ma table ne se vide pas j'ai une erreur indiquant :

La requête paramétré 'blabla' attend le paramètre @Parameter1, qui n'a pas été fourni

la requête en question a uniquement deux paramètres, celle d'avant trois.

requete : INSERT [champ1],[champ2],[champ3],[champ4],[champ5],[champ6]
VALUES (@varchamp1,1,2,@varchamp2,5,6)

toutes mes déclarations :
public string Cible = "";
        public string CSecteur = "";
        public byte EtatVal = 0;
        public Boolean Found = false;
        public string From = "";
        public string GroupBy = "";
        public string Into = "";
        public string IntoDbf = "";
        public uint NbErrTransac = 0;
        public int NbLigne = 0;
        public int NomConnection = 0; // a transformer en pointeur        
        public string OrderBy = "";
        public uint ResConnect = 0;
        public string Select = "";
        public string SourceDbf = "";
        public string Type = "";
        public string Values = "";
        public string Where = "";
        public string XExec = "";
        public string XInto = "";
        public string XSet = "";
        public string XVarVal="";
        public string XValues ="";
        public string XVar="";
        public string XVal="";
        
        ArrayList Tabstring = new ArrayList();
        string[] Tabmot;
        char[] tonTableaudeChar;
        SqlConnection connection ;
        SqlCommand command;
        SqlTransaction transaction;
        DataSet Dataset = new DataSet();
        Boolean trouve = false;
        SqlParameter[] TabParam= new SqlParameter[50];
        int nb_param = 0;
        int nb_string = 0;


la table de param doit se vider entre chaque requêtes (car elles comportent pas forcement le même nombre de paramètres ^^)
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
15 avril 2011 à 17:46
je confirme pour clear() et new ^^
mais j'ai toujours le souci :s je ne sais pas vraiment d'où il peu venir

voici en entier ma procédure d’exécution :
 public void Execution(string XExec)
        {

        try
        {
            
            for (int z = 0; XExec.Length > z; z++)
            {
                if (XExec[z] == '?')
                {
                    tonTableaudeChar = XExec.ToCharArray();
                    tonTableaudeChar[z] = '@';
                    XExec = new String(tonTableaudeChar);
                }
            }

            Console.Write("\n\nCommande : " + XExec + " \n\n");

            //-------------------------------TEST - solution 1 - TEST ----------------------------------

            parser(XExec); // choppe toutes mes variables ? quelquechose dans un Tabstring sans le ?
            for (int i = 0; i < nb_string; i++) // créé les parametres
            {
                Console.Write("\n\nDebut du test d'existence du paramètre "+Tabstring[i]+" dans TableDefini");
                trouve = existe(Tabstring[i].ToString());
                
                if (!trouve)
                {
                    Console.Write("\nParamètre " + Tabstring[i] + " non trouvé, début de la phase de construction du paramètre");
                    TabParam[i] = GetParameter(Tabstring[i].ToString()); // créé le param
                    command.Parameters.Add(ChercheParam(Tabstring[i].ToString())); // ajoute le parametre a la commande 
                    TableDefini.Add(Tabstring[i].ToString());
                    Console.Write("\nCréation de : " + TabParam[i].ToString());
                }
                else
                {
                    command.Parameters.Add(ChercheParam(Tabstring[i].ToString())); // ajoute le parametre a la commande 
                    Console.Write("\nLe paramètre " + Tabstring[i] + " existe, valeur éditée!");
                }
            
            }

            //------------------------------ FIN TEST - solution 1 - FIN TEST --------------------------

            command.CommandText = XExec;
            Console.Write("\n\ncommandetext : " + command.CommandText);
            Console.Write("\n\nAffectation des parametres a la commande .... execution");
            SqlDataReader reader = command.ExecuteReader();
            this.Found =reader.HasRows;
            reader.Close();

            if (this.Type == "SELECT")
            {
                 Dataset = RecupDataSet(command);
                 Console.Write("test => SELECT");
                 for (int i = 0; i <= Dataset.Tables[0].Rows.Count - 1; i++)
                {
                    MessageBox.Show(Dataset.Tables[0].Rows[i].ItemArray[0] + " -- " + Dataset.Tables[0].Rows[i].ItemArray[1]);
                }           
            }
            else if (this.Type == "COUNT")
            {
                Dataset = RecupDataSet(command);
                Console.Write("test => SELECT");
                this.NbLigne = (int)Dataset.Tables[0].Rows[0][0];
            }
            
            
            Console.Write("\n\nCommande executée, appuyez sur une touche pour continuer...\n");
            Console.ReadKey();
        }
        catch (Exception e)
        {
                    this.NbErrTransac++;
                    MessageBox.Show(e.ToString());
                }
        }



public void parser(string BOOM)
        {
            string chaine;
            string chainetmp;
            string mot;
            int position,k,save=0;

          
                chaine = BOOM;
                mot = "";
                position 0; k 0;
                
            Console.Write("\n\nDebut du parser ....");
                for (int z = 0; chaine.Length > z; z++)
                {
                    if (chaine[z] == '?')
                    {
                        tonTableaudeChar = chaine.ToCharArray();
                        tonTableaudeChar[z] = '@';
                        chaine = new String(tonTableaudeChar);
                    }
                }

                 
                for (int i = 0; chaine.Length > i; i++)
                {
                    if (chaine[i] == '@')
                    {
                        position = i;
                        chainetmp = chaine.Substring(position + 1);
                        for (int j = 0; chainetmp.Length > j; j++)
                        {
                            if (chainetmp[j] != ',' && chainetmp[j] != ' ' && chainetmp[j] != ')')
                            {

                                mot = mot + chainetmp[j];
                                k++;
                            }
                            else
                            {
                                save = k;
                                k = 0;
                                break;
                            }

                        }

                        Console.Write("\n\nVariable découverte : " + mot.Trim() + ", test d'existence dans Tabstring");
                        if (!(existestring(mot.Trim())))
                        {
                        Console.Write("\nParser : "+ mot.Trim()+" stocké dans Tabstring");
                        Tabstring.Add(mot.Trim());
                        mot = "";
                        save = 0;
                        this.nb_string++;
                        }
                        else
                        {
                        Console.Write("\nParser : mot existant dans Tabstring, non stocké");
                            mot = "";
                            save=0;
                        }

                    }
                }
                Console.Write("\n\nFin du parser ....");
            
        }

 private SqlParameter ChercheParam(string varName)
        {
            SqlParameter param = new SqlParameter();

            FieldInfo field = typeof(Program).GetField(varName, BindingFlags.NonPublic | BindingFlags.Static);

            if (field == null)
                throw new ArgumentException("La variable '" + varName + "' n'est pas définie dans la classe Program");

            Console.Write("\n\nDebut de la recuperation de @" + varName + " dans TabParam ...");
            for (int i = 0; nb_param> i; i++)
            {
                Console.Write("\nrecherche de : @" + varName + " dans TabParam : " +TabParam[i]);
                if (TabParam[i].ParameterName == '@'+varName)
                {
                    Console.Write("\nrecherche : parametre trouve : @" + varName+" .. récuperation...");
                    param = TabParam[i];
                    param.Value = field.GetValue(varName);
                }
            }

            return param;
        }


private Boolean existe(string varName)
        {
            Boolean hum=false;
            Console.Write("\n\nRecherche du parametre : @" + varName + " dans TableDefini ...");
            for (int i = 0; TableDefini.Count > i; i++)
                if (varName == TableDefini[i].ToString())
                {
                    Console.Write("\nParametre " + varName + " déja existant : "+TableDefini[i].ToString()  );
                    hum = true;
                    break;
                }
                else
                {
                    hum = false;
                    Console.Write("\nParametre " + varName + " non trouvé : " + TableDefini[i].ToString() );
                }
            
            Console.Write("\nFin recherche");
            if (TableDefini.Count == 0)
                Console.Write(" : aucun element dans TableDefini !");
            return hum;
        }


public Boolean existestring(string varName)
        {
            Boolean hum = false;
            Console.Write("\n\nRecherche de la chaine : " + varName + " dans la Tabstring");
            for (int i = 0; Tabstring.Count > i; i++)
                if (varName == Tabstring[i].ToString())
                {
                    Console.Write("\nChaine" + varName + " déja existante : " + Tabstring[i].ToString() );
                    hum = true;
                    break;
                }
                else
                {
                    hum = false;
                    Console.Write("\nChaine " + varName + " non trouvée : " + Tabstring[i].ToString() );
                }

            Console.Write("\nFin recherche");
            if (Tabstring.Count == 0)
                Console.Write(" : aucun element dans Tabstring !");
            return hum;
        }


Voila grosso-modo ce que ça donne ça fait gros pavé et une soupe de code inutile mais bon

J'ai rajouté pas mal de Console.Write pour débuger le truc ^^, tout s’exécute parfaitement(enfin dans le bon ordre, avec les bonnes recherches)
La première requête marche sans problème, et toutes les suivantes donnent le même problème :(


merci de m'éclairer si possible :)
0
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
15 avril 2011 à 18:05
Wow

Je vais tenter de débugger tout ça...

Pourrais-tu me donner un exemple de valeur pour la variable XExec passé à la méthode Execution() s'il te plait !?

Première remarque d'ailleur; tu peux remplacer :
char[] tonTableaudeChar;
for (int z = 0; XExec.Length > z; z++)
{
    if (XExec[z] == '?')
    {
        tonTableaudeChar = XExec.ToCharArray();
        tonTableaudeChar[z] = '@';
        XExec = new String(tonTableaudeChar);
    }
}


par : XExec = XExec.Replace( '?', '@' );
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
15 avril 2011 à 18:44
Re-bonjour !

Alors une requête sera de type SQL Server

XExec="SELECT * FROM [table] WHERE var=?mavar AND truc=?truc" dans class.cs

dans programme.cs :

private static int mavar = 0;
private static string truc = "chouette";


voila ^^

merci pour l'astuce pour le remplacement ^^


Si il te faut tout le code en entier pour les deux classes (environ 800 lignes) je te le passerais ^^

merci ;)
0
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
15 avril 2011 à 19:03
Si j'ai bien compris le but de ton code;
Tu es sensé pouvoir exécuter une requête du genre :

SELECT var1, var2, var3 FROM [TableTruc] WHERE ChampToto = @var4

donc avant d'exécuter la requête tu dois affecter la liste de SqlParameter avec les valeurs des champs. donc si ta variable 'var4' contient la valeur "bidule" tu exécutera la requête suivante :

SELECT var1, var2, var3 FROM [TableTruc] WHERE ChampToto = "bidule"

et ensuite tu devras affecter les valeurs de retour à tes autres variables.
Donc la variable var1 contiendra la valeur de la colonne var1 issue de la base, idem pour var2 et var3...

Es-ce bien cela ??
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
15 avril 2011 à 20:49
Je vais te donner le code complet de mon programme pour que tu puisse voir

Pour les requêtes :

Dans ma classe j'ai plusieurs attributs comme type, into, value, where, etc.

je remplit donc ma classe comme ceci (dans le programme principal)

Classe Classe = new Classe();

Classe.RAZ;
Classe.connect();
Classe.type="SELECT";
Classe.select="*";
Classe.from="maTable, uneautretable";
Classe.execute()
Classe.disconnect();

donnera cette requete : SELECT * FROM maTable, unautretable


Classe.RAZ;
Classe.connect();
Classe.type="DELETE";
Classe.from="maTable, uneautretable";
Classe.where="attribut=?var_attribut AND truc=?truc" // je doit respecter le '?' je fait une conversion de //tout les ? en @
Classe.execute()
Classe.disconnect();

donnera : DELETE FROM maTable, uneautretable WHERE attribut=?var_attribut AND truc=?truc

ensuite a léxecution avec un executereader et les parametres ?var_attribut et ?truc (qui donnerons d'ici la
@var_attribut et @truc pour que ca marche en C#)

donne sur le serveur lui meme sera executé DELETE FROM maTable, uneautretable WHERE attribut=3 AND truc="truc"

par exemple

je te passe tout le code des que j'ai fini d'installer visual C#
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
15 avril 2011 à 20:54
voila le code dans son integralitée


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Reflection;



namespace ConsoleApplication1
{

class Program
{
private static int G_Etendue = 7;
private static string G_CAction = "toto";
private static int SQL_DEbAph = 1;
private static int SQL_DebApMin = 1;
private static int SQL_ApH = 1;
private static int SQL_ApMin = 1;

static void Main(string[] args)
{

LANGAGE2SQL L_LANGAGE2SQL = new LANGAGE2SQL();



//------------------------------DELETE-----------------------------------
L_LANGAGE2SQL.RAZ();
L_LANGAGE2SQL.Connect();
L_LANGAGE2SQL.CSecteur = "";
L_LANGAGE2SQL.EtatVal = 1;
L_LANGAGE2SQL.Type = "DELETE";
L_LANGAGE2SQL.Cible = "[DBSoft].[dbo].[DateAff]";
L_LANGAGE2SQL.Where = "Etendue=?G_Etendue AND CAction=?G_CAction AND Etendue=?G_Etendue";
L_LANGAGE2SQL.Execute();


//-----------------------------------------------------------------------
Console.Write("\n Delete résolu, attente...\n");
Console.ReadKey();

//------------------------------INSERT-----------------------------------

L_LANGAGE2SQL.RAZ();
L_LANGAGE2SQL.EtatVal = 1;
L_LANGAGE2SQL.Type="INSERT";
L_LANGAGE2SQL.Cible="[DBSoft].[dbo].[DateAff]";
L_LANGAGE2SQL.Into="[CAction] ,[CSecteur] ,[DForm] ,[Etendue] ,[Heure] ,[DebQ] ,[CModule] ,[EtatVal] ,[CForm] ,[FKDateForm]";
L_LANGAGE2SQL.Values = "?G_CAction,9,8,?G_Etendue,6,5,4,3,2,1";
L_LANGAGE2SQL.Execute();

//-----------------------------------------------------------------------
Console.Write("\n insert résolu, attente ...\n");
Console.ReadKey();

// -----------------------------SELECT-----------------------------------


L_LANGAGE2SQL.RAZ();
L_LANGAGE2SQL.CSecteur = "";
L_LANGAGE2SQL.EtatVal = 3;
L_LANGAGE2SQL.Type = "SELECT";
L_LANGAGE2SQL.Select = "*";
L_LANGAGE2SQL.From = "[DBSoft].[dbo].[DateAff]";
L_LANGAGE2SQL.Where = "";
L_LANGAGE2SQL.OrderBy = "";
L_LANGAGE2SQL.GroupBy = "";
L_LANGAGE2SQL.IntoDbf = "T_POIN";
L_LANGAGE2SQL.Execute();
Console.Write("\n Select résolu found = " + L_LANGAGE2SQL.Found + " , attente...\n");


//-----------------------------------------------------------------------
Console.Write("\n SELECT résolu, attente ...\n");
Console.ReadKey();

//----------------------- UPDATE ----------------------------------------
L_LANGAGE2SQL.RAZ();
L_LANGAGE2SQL.EtatVal = 0;
L_LANGAGE2SQL.Type="UPDATE";
L_LANGAGE2SQL.Cible="DateForm";
L_LANGAGE2SQL.Into="DEbAph,DebApMin,ApH,ApMin";
L_LANGAGE2SQL.Where="CAction=?G_CAction and etendue=?G_Etendue";
L_LANGAGE2SQL.Values = "?SQL_DEbAph,?SQL_DebApMin,?SQL_ApH,?SQL_ApMin";
L_LANGAGE2SQL.Execute();

//-----------------------------------------------------------------------
Console.Write("\n update résolu, attente ...\n");
Console.ReadKey();
// -------------------------------------- COUNT ----------------------------

L_LANGAGE2SQL.RAZ();
L_LANGAGE2SQL.CSecteur = "";
L_LANGAGE2SQL.EtatVal = 3;
L_LANGAGE2SQL.Type = "COUNT";
L_LANGAGE2SQL.From = "[DBSoft].[dbo].[DateAff]";
L_LANGAGE2SQL.OrderBy = "";
L_LANGAGE2SQL.GroupBy = "";
L_LANGAGE2SQL.IntoDbf = "T_POIN";
L_LANGAGE2SQL.Execute();
Console.Write("\n Count résolu appuyez sur une touche...");
L_LANGAGE2SQL.DisConnect();

Console.Write(" Nombre de ligne : " + L_LANGAGE2SQL.NbLigne);
//--------------------------------------------------------------------------
Console.Write("\n count résolu, attente ...\n");
Console.ReadKey();




Console.ReadKey();

}
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;


namespace ConsoleApplication1
{
class LANGAGE2SQL
{

public string Cible = "";
public string CSecteur = "";
public byte EtatVal = 0;
public Boolean Found = false;
public string From = "";
public string GroupBy = "";
public string Into = "";
public string IntoDbf = "";
public uint NbErrTransac = 0;
public int NbLigne = 0;
public int NomConnection = 0; // a transformer en pointeur
public string OrderBy = "";
public uint ResConnect = 0;
public string Select = "";
public string SourceDbf = "";
public string Type = "";
public string Values = "";
public string Where = "";
public string XExec = "";
public string XInto = "";
public string XSet = "";
public string XVarVal="";
public string XValues ="";
public string XVar="";
public string XVal="";

ArrayList Tabstring = new ArrayList();
string[] Tabmot;
char[] tonTableaudeChar;
SqlConnection connection ;
SqlCommand command;
SqlTransaction transaction;
DataSet Dataset = new DataSet();
Boolean trouve = false;
SqlParameter[] TabParam= new SqlParameter[50];
int nb_param = 0;
int nb_string = 0;

ArrayList TableDefini = new ArrayList();

// Affecte la valeur issue de la base de données à une variable de la classe Program
private void SetValue(string varName, DataRow dataRow)
{
FieldInfo field = typeof(Program).GetField(varName, BindingFlags.NonPublic | BindingFlags.Static);

if (field == null)
throw new ArgumentException("La variable '" + varName + "' n'est pas définie dans la classe Program");

field.SetValue(null, dataRow[varName]);
}

//cherche le nom du parametre dans la tableDefini, renvoie true si trouvé
private Boolean existe(string varName)
{
Boolean hum=false;
Console.Write("\n\nRecherche du parametre : @" + varName + " dans TableDefini ...");
for (int i = 0; TableDefini.Count > i; i++)
if (varName == TableDefini[i].ToString())
{
Console.Write("\nParametre " + varName + " déja existant : "+TableDefini[i].ToString() );
hum = true;
break;
}
else
{
hum = false;
Console.Write("\nParametre " + varName + " non trouvé : " + TableDefini[i].ToString() );
}

Console.Write("\nFin recherche");
if (TableDefini.Count == 0)
Console.Write(" : aucun element dans TableDefini !");
return hum;
}

public Boolean existestring(string varName)
{
Boolean hum = false;
Console.Write("\n\nRecherche de la chaine : " + varName + " dans la Tabstring");
for (int i = 0; Tabstring.Count > i; i++)
if (varName == Tabstring[i].ToString())
{
Console.Write("\nChaine" + varName + " déja existante : " + Tabstring[i].ToString() );
hum = true;
break;
}
else
{
hum = false;
Console.Write("\nChaine " + varName + " non trouvée : " + Tabstring[i].ToString() );
}

Console.Write("\nFin recherche");
if (Tabstring.Count == 0)
Console.Write(" : aucun element dans Tabstring !");
return hum;
}

// Retourne un SqlParameter affecté de la valeur d'une variable d'après son nom
private SqlParameter GetParameter(string varName)
{

FieldInfo field = typeof(Program).GetField(varName, BindingFlags.NonPublic | BindingFlags.Static);

if (field == null)
throw new ArgumentException("La variable '" + varName + "' n'est pas définie dans la classe Program");

SqlDbType dbType;
switch (field.FieldType.FullName)
{
case ("System.String"):
dbType = SqlDbType.VarChar;
break;

case ("System.Int32"):
dbType = SqlDbType.Int;
break;

case ("System.DateTime"):
dbType = SqlDbType.DateTime;
break;

default:
throw new ArgumentException("Une variable de Type '" + field.FieldType.FullName + "' ne peut pas être converti en SqlDbType");
}


SqlParameter parameter = new SqlParameter("@"+varName, dbType);
parameter.Value = field.GetValue(null);
nb_param++;

return parameter;
}

private SqlParameter ChercheParam(string varName)
{
SqlParameter param = new SqlParameter();

FieldInfo field = typeof(Program).GetField(varName, BindingFlags.NonPublic | BindingFlags.Static);

if (field == null)
throw new ArgumentException("La variable '" + varName + "' n'est pas définie dans la classe Program");

Console.Write("\n\nDebut de la recuperation de @" + varName + " dans TabParam ...");
for (int i = 0; nb_param> i; i++)
{
Console.Write("\nrecherche de : @" + varName + " dans TabParam : " +TabParam[i]);
if (TabParam[i].ParameterName == '@'+varName)
{
Console.Write("\nrecherche : parametre trouve : @" + varName+" .. récuperation...");
param = TabParam[i];
param.Value = field.GetValue(varName);
}
}

return param;
}

public LANGAGE2SQL(){
}

public void Connect()
{
try
{


// ------------------------------------------ connexion générale -----------------------------------------------
connection = new SqlConnection();
connection.ConnectionString = "server=sctest3\\SQLDEV; uid=softc; pwd=IG7604; database=DBSoft";
connection.Open();

command = connection.CreateCommand();

// Lance une transaction
transaction = connection.BeginTransaction("Transaction");

// on assigne connection et transaction a une requete
command.Connection = connection;
command.Transaction = transaction;

this.NbErrTransac = 0;
TableDefini.Clear();

}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}


}

public void DisConnect()
{
try
{
Console.Write("\nDeconnexion !\n");

if (this.NbErrTransac == 0)
{
transaction.Commit();
Console.Write("C'est tout bon ! Commit !\n");
}
else
{
transaction.Rollback();
Console.Write("Il y a eu une erreur ! Rollback !\n");
}

connection.Close();
}
catch
{
Console.Write(" Erreur transaction ");
connection.Close();
}



}

public void rollbackurgence()
{
transaction.Rollback();
Console.Write(" rollback urgent effectué ");
}

public void Execute()
{
string XTypeSQL = this.Type;
switch (XTypeSQL)
{
case "SELECT":
this.P_selectSQL();
break;
case "DELETE":
this.P_DeleteSQL();
break;
case "INSERT" :
this.P_InsertSQL();
break;
//case "APPENDFROM" : this.P_AppendFromSQL ;
case "UPDATE" :
this.P_UpdateSQL();
break;
case "COUNT" :
this.P_CountSQL();
break;

}
}

public void RAZ()
{
this.Cible = "";
this.CSecteur = "";
this.EtatVal = 0;
this.From = "";
this.GroupBy = "";
this.Into = "";
this.IntoDbf = "";
this.NbLigne = 0;
this.NomConnection = 0; // a transformer en pointeur
this.OrderBy = "";
this.ResConnect = 0;
this.Select = "";
this.SourceDbf = "";
this.Type = "";
this.Values = "";
this.Where = "";
this.Tabstring.Clear();
this.nb_string = 0;
TabParam = new SqlParameter[50];
nb_param = 0;

}

public void parser(string BOOM)
{
string chaine;
string chainetmp;
string mot;
int position,k,save=0;


chaine = BOOM;
mot = "";
position 0; k 0;

Console.Write("\n\nDebut du parser ....");
for (int z = 0; chaine.Length > z; z++)
{
if (chaine[z] == '?')
{
tonTableaudeChar = chaine.ToCharArray();
tonTableaudeChar[z] = '@';
chaine = new String(tonTableaudeChar);
}
}


for (int i = 0; chaine.Length > i; i++)
{
if (chaine[i] == '@')
{
position = i;
chainetmp = chaine.Substring(position + 1);
for (int j = 0; chainetmp.Length > j; j++)
{
if (chainetmp[j] != ',' && chainetmp[j] != ' ' && chainetmp[j] != ')')
{

mot = mot + chainetmp[j];
k++;
}
else
{
save = k;
k = 0;
break;
}

}

Console.Write("\n\nVariable découverte : " + mot.Trim() + ", test d'existence dans Tabstring");
if (!(existestring(mot.Trim())))
{
Console.Write("\nParser : "+ mot.Trim()+" stocké dans Tabstring");
Tabstring.Add(mot.Trim());
mot = "";
save = 0;
this.nb_string++;
}
else
{
Console.Write("\nParser : mot existant dans Tabstring, non stocké");
mot = "";
save=0;
}

}
}
Console.Write("\n\nFin du parser ....");

}

public void P_DeleteSQL()
{
try
{

if (!(this.Cible == ""))
{
//Cible
string XExec = "DELETE " + this.Cible;

// From
if (!(this.From == ""))
XExec = XExec + " FROM " + this.From;


// Where
string Xwhere = "";
if (!(this.Where == ""))
{

Xwhere = this.Where;
}
string chainetmp;
if (this.EtatVal >= 0)
{
chainetmp = this.EtatVal.ToString().TrimStart();
chainetmp = this.EtatVal.ToString().TrimEnd();
Xwhere = Xwhere + " AND Etatval=" + chainetmp;

}
if (!(Xwhere == ""))
{
if (Xwhere.Substring(0, 5) == " AND ")
{
Xwhere = Xwhere.Substring(5);

}
XExec = XExec + " WHERE " + Xwhere;
}

Execution(XExec);

}
else
{
this.NbErrTransac++;
}


}
catch (Exception e)
{
this.NbErrTransac++;
MessageBox.Show(e.ToString());
}
}

public void P_selectSQL()
{
try
{
// Seulement si Select, From et Into sont définis!!!
if (!(this.Select "") && !(this.From "") && !(this.IntoDbf == ""))
{

// Select
XExec = "SELECT " + this.Select;

// From
XExec = XExec + " FROM " + this.From;

// Where
string Xwhere = "";
string XN4CSecteur;

int XBlanc;
if (!(this.Where == ""))
{

Xwhere = "(" + this.Where + ")";

}
if (!(this.CSecteur == ""))
{
XN4CSecteur = "CSecteur";

XBlanc = this.From.IndexOf(" ");
if (XBlanc > 0)
XN4CSecteur = this.From.Substring(1, XBlanc - 1) + "." + XN4CSecteur;


Xwhere = Xwhere + " AND " + XN4CSecteur + "='" + this.CSecteur + "'";
}
string chainetmp;
if (this.EtatVal >= 0)
{
chainetmp = this.EtatVal.ToString().Trim();
Xwhere = Xwhere + " AND Etatval=" + chainetmp;

}
if (!(Xwhere == ""))
{
if (Xwhere.Substring(0, 5) == " AND ")
{
Xwhere = Xwhere.Substring(5);

}
XExec = XExec + " WHERE " + Xwhere;
}

// Group By
if (!(this.GroupBy == ""))
XExec = XExec + " GROUP BY " + this.GroupBy;

// Order By
if (!(this.OrderBy == ""))
XExec = XExec + " ORDER BY " + this.OrderBy;


Execution(XExec);

}
else
{
this.NbErrTransac++;
}

}
catch (Exception e)
{
this.NbErrTransac++;
MessageBox.Show(e.ToString());
}
}

public void P_InsertSQL()
{
try
{
// Seulement si Cible, Into et Values sont définis!!!
if (!(this.Cible "") && !(this.Into "") && !(this.Values == ""))
{
// Cible
XExec = "INSERT " + this.Cible;

// Into
XExec = XExec + " (" + this.Into + ")";

// Values
if (this.Values.Substring(0, 1) == "(")
{

XExec = XExec + " VALUES " + this.Values; // Ajout multiple
}
else
{

XExec = XExec + " VALUES (" + this.Values + ")"; // Ajout simple
}


}
else
{
this.NbErrTransac++;
}

Execution(XExec);

}
catch (Exception e)
{
this.NbErrTransac++;
MessageBox.Show(e.ToString());
}

}

public void P_UpdateSQL()
{
// Seulement si Cible, Into et Values sont définis!!!
if (!(this.Cible "") && !(this.Into "") && !(this.Values == ""))
{
// Cible
XExec="UPDATE "+this.Cible;

// Création de la clause SET
string[] TabInto;
string[] TabValues;
XInto=this.Into;
XValues=this.Values;
XSet="";
XVarVal="";
string yahou="";

// recupération des mots

TabInto = Recupmot(XInto);
TabValues = Recupmot(XValues);

for (int i = 0; Tabmot.Length-1 > i; i++)
{
yahou = yahou + TabInto[i] + "=" + TabValues[i] + ",";
}

// Set
XExec = XExec + " SET " + yahou.Substring(0,yahou.Length - 1);

// Where
string XWhere="";
if (!(this.Where==""))
XWhere="("+this.Where+")";

if (!(this.CSecteur==""))
XWhere=XWhere+" AND CSecteur='"+this.CSecteur+"'";

if (this.EtatVal>=0)
XWhere=XWhere+" AND Etatval="+this.EtatVal.ToString().Trim();

if (!(XWhere==""))
{
if (XWhere.Substring(0,5)==" AND ")
XWhere=XWhere.Substring(6);


XExec=XExec+" WHERE "+XWhere;
}


Execution(XExec);

}

}

public void P_CountSQL()
{
try
{
// Seulement si Select, From et Into sont définis!!!
if (!(this.From "") && !(this.IntoDbf ""))
{

// Select
XExec = "SELECT COUNT(*) As Nb";

// From
XExec = XExec + " FROM " + this.From;

// Where
string Xwhere = "";
string XN4CSecteur;

int XBlanc;
if (!(this.Where == ""))
{
Xwhere = "(" + this.Where + ")";

}
if (!(this.CSecteur == ""))
{
XN4CSecteur = "CSecteur";

XBlanc = this.From.IndexOf(" ");
if (XBlanc > 0)
XN4CSecteur = this.From.Substring(1, XBlanc - 1) + "." + XN4CSecteur;


Xwhere = Xwhere + " AND " + XN4CSecteur + "='" + this.CSecteur + "'";
}
string chainetmp;
if (this.EtatVal >= 0)
{
chainetmp = this.EtatVal.ToString().Trim();
Xwhere = Xwhere + " AND Etatval=" + chainetmp;

}
if (!(Xwhere == ""))
{
if (Xwhere.Substring(0, 5) == " AND ")
{
Xwhere = Xwhere.Substring(5);

}
XExec = XExec + " WHERE " + Xwhere;
}

// Group By
if (!(this.GroupBy == ""))
XExec = XExec + " GROUP BY " + this.GroupBy;

// Order By
if (!(this.OrderBy == ""))
XExec = XExec + " ORDER BY " + this.OrderBy;


Execution(XExec);

}
else
{
this.NbErrTransac++;
}

}
catch (Exception e)
{
this.NbErrTransac++;
MessageBox.Show(e.ToString());
}

}

public string[] Recupmot(string chaine)
{
Tabmot = new string[30];

Tabmot=chaine.Split(',');

return Tabmot;
}

public void Execution(string XExec)
{

try
{

for (int z = 0; XExec.Length > z; z++)
{
if (XExec[z] == '?')
{
tonTableaudeChar = XExec.ToCharArray();
tonTableaudeChar[z] = '@';
XExec = new String(tonTableaudeChar);
}
}

Console.Write("\n\nCommande : " + XExec + " \n\n");

//-------------------------------TEST - solution 1 - TEST ----------------------------------

parser(XExec); // choppe toutes mes variables ? quelquechose dans un Tabstring sans le ?
for (int i = 0; i < nb_string; i++) // créé les parametres
{
Console.Write("\n\nDebut du test d'existence du paramètre "+Tabstring[i]+" dans TableDefini");
trouve = existe(Tabstring[i].ToString());

if (!trouve)
{
Console.Write("\nParamètre " + Tabstring[i] + " non trouvé, début de la phase de construction du paramètre");
TabParam[i] = GetParameter(Tabstring[i].ToString()); // créé le param
command.Parameters.Add(ChercheParam(Tabstring[i].ToString())); // ajoute le parametre a la commande
TableDefini.Add(Tabstring[i].ToString());
Console.Write("\nCréation de : " + TabParam[i].ToString());
}
else
{
command.Parameters.Add(ChercheParam(Tabstring[i].ToString())); // ajoute le parametre a la commande
Console.Write("\nLe paramètre " + Tabstring[i] + " existe, valeur éditée!");
}

}

//------------------------------ FIN TEST - solution 1 - FIN TEST --------------------------

command.CommandText = XExec;
Console.Write("\n\ncommandetext : " + command.CommandText);
Console.Write("\n\nAffectation des parametres a la commande .... execution");
SqlDataReader reader = command.ExecuteReader();
this.Found =reader.HasRows;
reader.Close();

if (this.Type == "SELECT")
{
Dataset = RecupDataSet(command);
Console.Write("test => SELECT");
for (int i = 0; i
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
18 avril 2011 à 00:53
Re-bonjour

As tu eu le temps de voir plus en detail et de trouver une solution ? ce n'est toujours pas mon cas malheureusement :(
0
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
18 avril 2011 à 05:56
Salut,

J'ai pas pu y toucher ce WE... mais j'attaque ça de suite ;)

A première vu, j'ai vu quelques corrections à appliquer... qui allégeront ton code. Mais pour l'instant faisons en sorte que ça marche !

J'te tiens au jus !!!
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
18 avril 2011 à 08:23
En fait j'avais justement créé tabledefini pour que entre les requêtes je garde une liste de tout les paramètres existants, uniques dans la table (d’où un petit test d'unicité pour les ajouts) car on peut éditer les paramètres et du coup on ne les recréé pas (j'avais eu une erreur : paramètre déjà déclaré, alors je me suis dit je vais les stocker et plus les déclarer )

Après c'est le premier code que je fais dans ce langage donc effectivement j'ai peut être fait une soupe pour des choses très simples

en tout cas merci de ton aide ;)
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
18 avril 2011 à 09:17
Rere !

Avec command.Parameters.Clear(); juste après l’exécution de la commande avec le reader ça marche ^^

Maintenant alléger le code T_T
0
lucss4 Messages postés 23 Date d'inscription lundi 29 octobre 2007 Statut Membre Dernière intervention 21 avril 2011
18 avril 2011 à 11:46
C'est re-moi !

J'ai décidé de partir sur une petite interface graphique...

je suis donc entrain de bosser sur ce code :
 private void Form1_Load(object sender, EventArgs e)
        {
            LANGAGE2SQL L_LANGAGE2SQL = new LANGAGE2SQL();

            L_LANGAGE2SQL.RAZ();
            L_LANGAGE2SQL.Connect();
            L_LANGAGE2SQL.CSecteur = "";
            L_LANGAGE2SQL.EtatVal = 3;
            L_LANGAGE2SQL.Type = "SELECT";
            L_LANGAGE2SQL.Select = "*";
            L_LANGAGE2SQL.From = "[DBSoft].[dbo].[DateAff]";
            L_LANGAGE2SQL.Where = "Etendue=?G_Etendue";
            L_LANGAGE2SQL.OrderBy = "";
            L_LANGAGE2SQL.GroupBy = "";
            L_LANGAGE2SQL.IntoDbf = "T_POIN";
            L_LANGAGE2SQL.Execute();

            DataSet dataset1 = new DataSet();
            dataset1 = (L_LANGAGE2SQL.RecupDataSet(L_LANGAGE2SQL.command));



            dataGridView1.DataSource = dataset1;
            dataGridView1.DataMember = "Table";
            dataGridView1.Columns["PKDateAff"].ReadOnly = true;
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
                listBox.Items.Add(dataGridView1.Columns[i].Name);

            L_LANGAGE2SQL.DisConnect();

        }

    

        private void btn_select_Click(object sender, EventArgs e)
        {
            
            
            Label_test.Text = "SELECTFAIT";
            

            
        }


Malheureusement j'ai comme erreur au chargement de ma fenêtre :

La variable scalaire @G_Etendue doit être déclarée

Kesaco ? ça marchais très bien tout juste avant :(

Pour ce qui est de l'affichage tout fonctionne je l'ai testé tantôt ..

Merci d'avance pour les éclaircissements a ce sujet !
0
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 8
18 avril 2011 à 12:22
Vérifie qu'avant d'exécuter la commande SQL tu ais bien mis command.CommandType = CommandType.Text;
C'est peut être qu'il pense exécuter une procédure stockée...
Autrement je vois pas !
0
Rejoignez-nous