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 :

Probleme avec ma classe de connexion


Sujet :

ASP.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    Par défaut Probleme avec ma classe de connexion
    Bonjourà tous,
    Je suis débutant en asp.net et je veux créer une classe de connexion à ma BD mysql en utilisant ODBC. Pour cela j'ai télécharger une code écrit en vb.net sur le web que j'ai essayé de traduire en C#.
    Le code est le suivant:
    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 OdbcDataReader myODBCreader(string ReqSql, string CnxStrings)
        { 
            OdbcConnection myODBCConnexion = new OdbcConnection(CnxStrings);
     
            try
            {
            OdbcCommand myCommand =new OdbcCommand();
            OdbcDataReader myDataReader;
            OdbcConnection.ReleaseObjectPool();
            myODBCConnexion.Open();
            myCommand.Connection=myODBCConnexion;
            myCommand.CommandText= ReqSql;
            myDataReader= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            return (OdbcDataReader)result=myDataReader;
            OdbcConnection.ReleaseObjectPool();
            }
            catch (OdbcException myODBCexception)  
            {
                HttpContext.Current.Trace.Write(myODBCexception.ToString());
            }
            catch (Exception myException)
            {
                HttpContext.Current.Trace.Write(myException.ToString());
            }
            finally
            {
            if (myODBCConnexion != null )
            {
            myODBCConnexion.Close();
            myODBCConnexion = null;
            }
            }
        }
    A la compilation, j'ai l'erreur suivante:
    The name 'result' does not exist in the current context
    Merci de bien vouloir m'aider.

  2. #2
    Membre confirmé
    Inscrit en
    Septembre 2006
    Messages
    76
    Détails du profil
    Informations forums :
    Inscription : Septembre 2006
    Messages : 76
    Par défaut
    Voici un code qui peut resoudre ton probleme

    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
     
    Imports System
    Imports System.Data
    Imports System.Data.Odbc
     
    '....                
    'Connection string for MyODBC 3.51
     
    Dim MyConString As String = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" +  _
    "DATABASE=munkh3y;" + "UID=root;" + "PASSWORD=;"
    Try
       Dim con As New OdbcConnection(MyConString) '
       con.Open()
       Dim MyCommand As New OdbcCommand()
       MyCommand.Connection = con
     
       MyCommand.CommandText = "SELECT * FROM munkh3y_users"
       Dim MyDataReader As OdbcDataReader
       MyDataReader = MyCommand.ExecuteReader()
       While MyDataReader.Read() 
          Response.Write(("Data:" + MyDataReader.GetString(1) + " " + MyDataReader.GetString(2)))
       End While
       MyDataReader.Close()
       con.Close()
    Catch ex As Exception 
       Response.Write(ex.Message);
    End Try

  3. #3
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    Par défaut
    Merci , je l'essaie et je te tiens informer.

  4. #4
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    Par défaut
    Merci beaucoup ça marche.Mais je me pose une autre question, pourquoi le retour de valeur ne passe pas dans ma fonction ci dessus.

  5. #5
    Membre confirmé
    Inscrit en
    Septembre 2006
    Messages
    76
    Détails du profil
    Informations forums :
    Inscription : Septembre 2006
    Messages : 76
    Par défaut
    il faut ajouter dans ta fonction la decalration de l'objet.

  6. #6
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    Par défaut
    Comment je fait car je déclare l'objet result en temps que OdbcDataReader mais ca ne marche pas.

  7. #7
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Citation Envoyé par sadem Voir le message
    Comment je fait car je déclare l'objet result en temps que OdbcDataReader mais ca ne marche pas.
    Tu parles de cette ligne?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (OdbcDataReader)result=myDataReader;
    Parce qu'il va falloir revoir les bases si c'est le cas

  8. #8
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    Par défaut
    Merci , mais c'est la cause de mon erreur car quand je mets (OdbcDataReader)result=myDataReader;
    return result;
    il me mets l'erreur suivante:
    The name 'result' does not exist in the current context

  9. #9
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Citation Envoyé par sadem Voir le message
    Merci , mais c'est la cause de mon erreur car quand je mets (OdbcDataReader)result=myDataReader;
    return result;
    il me mets l'erreur suivante:
    The name 'result' does not exist in the current context
    Tu lis les réponses précédentes?
    A aucun moment tu déclares result

  10. #10
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    Par défaut
    Si à ce niveau:
    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
    public OdbcDataReader myODBCreader(string ReqSql, string CnxStrings)
        { 
            OdbcConnection myODBCConnexion = new OdbcConnection(CnxStrings);
            
            try
            {
            OdbcCommand myCommand =new OdbcCommand();
            OdbcDataReader myDataReader;
            OdbcConnection.ReleaseObjectPool();
            myODBCConnexion.Open();
            myCommand.Connection=myODBCConnexion;
            myCommand.CommandText= ReqSql;
            myDataReader= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            return (OdbcDataReader)result=myDataReader;
            OdbcConnection.ReleaseObjectPool();
            }
            catch (OdbcException myODBCexception)  
            {
                HttpContext.Current.Trace.Write(myODBCexception.ToString());
            }
            catch (Exception myException)
            {
                HttpContext.Current.Trace.Write(myException.ToString());
            }
            finally
            {
            if (myODBCConnexion != null )
            {
            myODBCConnexion.Close();
            myODBCConnexion = null;
            }
            }
        }

  11. #11
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Citation Envoyé par sadem Voir le message
    Si à ce niveau:
    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
    public OdbcDataReader myODBCreader(string ReqSql, string CnxStrings)
        { 
            OdbcConnection myODBCConnexion = new OdbcConnection(CnxStrings);
            
            try
            {
            OdbcCommand myCommand =new OdbcCommand();
            OdbcDataReader myDataReader;
            OdbcConnection.ReleaseObjectPool();
            myODBCConnexion.Open();
            myCommand.Connection=myODBCConnexion;
            myCommand.CommandText= ReqSql;
            myDataReader= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            return (OdbcDataReader)result=myDataReader;
            OdbcConnection.ReleaseObjectPool();
            }
            catch (OdbcException myODBCexception)  
            {
                HttpContext.Current.Trace.Write(myODBCexception.ToString());
            }
            catch (Exception myException)
            {
                HttpContext.Current.Trace.Write(myException.ToString());
            }
            finally
            {
            if (myODBCConnexion != null )
            {
            myODBCConnexion.Close();
            myODBCConnexion = null;
            }
            }
        }
    Bah non, c'est pas comme ça qu'on déclare une variable

  12. #12
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    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
    34
    public OdbcDataReader myODBCreader(string ReqSql, string CnxStrings)
        {
            OdbcConnection myODBCConnexion = new OdbcConnection(CnxStrings);
            try
            {
                OdbcCommand myCommand = new OdbcCommand();
                OdbcDataReader myDataReader;
                OdbcConnection.ReleaseObjectPool();
                myODBCConnexion.Open();
                myCommand.Connection = myODBCConnexion;
                myCommand.CommandText = ReqSql;
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                (OdbcDataReader)result = myDataReader;
                return result;
                OdbcConnection.ReleaseObjectPool();
            }
            catch (OdbcException myODBCexception)
            {
                HttpContext.Current.Trace.Write(myODBCexception.ToString());
            }
            catch (Exception myException)
            {
                HttpContext.Current.Trace.Write(myException.ToString());
            }
            finally
            {
                if (myODBCConnexion != null)
                {
                    myODBCConnexion.Close();
                    myODBCConnexion = null;
                }
            }
        }
    En modifiant de cette facon , j'ai toujours la même erreur .
    En declarant :
    ODBCDataReader result;
    et apres en assignant myDataReader , j'ai toujours la même erreur lorsque je veux retourner la valeur.

  13. #13
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Citation Envoyé par sadem Voir le message
    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
    public OdbcDataReader myODBCreader(string ReqSql, string CnxStrings)
        {
            OdbcConnection myODBCConnexion = new OdbcConnection(CnxStrings);
            try
            {
                OdbcCommand myCommand = new OdbcCommand();
                OdbcDataReader myDataReader;
                OdbcConnection.ReleaseObjectPool();
                myODBCConnexion.Open();
                myCommand.Connection = myODBCConnexion;
                myCommand.CommandText = ReqSql;
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                (OdbcDataReader)result = myDataReader;
                return result;
                OdbcConnection.ReleaseObjectPool();
            }
            catch (OdbcException myODBCexception)
            {
                HttpContext.Current.Trace.Write(myODBCexception.ToString());
            }
            catch (Exception myException)
            {
                HttpContext.Current.Trace.Write(myException.ToString());
            }
            finally
            {
                if (myODBCConnexion != null)
                {
                    myODBCConnexion.Close();
                    myODBCConnexion = null;
                }
            }
        }
    En modifiant de cette facon , j'ai toujours la même erreur .
    En declarant :
    ODBCDataReader result;
    et apres en assignant myDataReader , j'ai toujours la même erreur lorsque je veux retourner la valeur.
    C'est pas comme ça non plus

  14. #14
    Membre confirmé
    Inscrit en
    Septembre 2007
    Messages
    77
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 77
    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
     
     try
            {
                OdbcCommand myCommand = new OdbcCommand();
                OdbcDataReader myDataReader;
                OdbcConnection.ReleaseObjectPool();
                myODBCConnexion.Open();
                myCommand.Connection = myODBCConnexion;
                myCommand.CommandText = ReqSql;
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                OdbcDataReader result;
                result = myDataReader;
                return result;
                OdbcConnection.ReleaseObjectPool();
            }
    Cette fois ci , l'erreur est la suivante :
    Error 2 'ODBCrequest.myODBCreader(string, string)': not all code paths return a value
    et un warning :
    Unreachable code detected o niveau de la ligne odbcconnection.releaseobjectpool();
    Est-ce que ma fonction est correcte?

Discussions similaires

  1. [POO] Problème avec les classes
    Par peypey dans le forum Langage
    Réponses: 8
    Dernier message: 03/05/2006, 15h05
  2. probleme avec la classe vector
    Par elekis dans le forum Langage
    Réponses: 4
    Dernier message: 12/04/2006, 16h25
  3. Probleme avec une class template
    Par lenectar dans le forum Langage
    Réponses: 2
    Dernier message: 01/03/2006, 10h49
  4. probleme avec la classe calendar
    Par fatmax dans le forum Collection et Stream
    Réponses: 6
    Dernier message: 04/10/2005, 17h04
  5. [apache] probleme avec le partage de connexion internet
    Par Delphy113 dans le forum Apache
    Réponses: 9
    Dernier message: 23/06/2005, 11h49

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