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# Discussion :

Problème connexion MSSQL / lancement requête


Sujet :

C#

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Mars 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 28
    Localisation : France

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2014
    Messages : 9
    Points : 8
    Points
    8
    Par défaut Problème connexion MSSQL / lancement requête
    Bonjour,

    Souhaitant migrer ma base de données MySQL vers une base de données SQL Server (j'ai bien entendu converti ma table à la syntaxe de SQL Server 2012 mais passant)

    j'ai donc commencer à retoucher le système de connexion de mon application j'ai remplacé toutes les fonctions de MySQL par celle de SQL Server 2012, hors actuellement je rencontre un soucis, mon application se connecte bien à la base de données SQL Server (yeah)
    mais quand celui-ci exécute une requête afin de pouvoir récupérer la totalités des logs de mes membres (cela fais ça sur n'importe quel requête) celui-ci m'affiche l'erreur

    "La référence d'objet n'est pas défini à une instance d'un objet " concernant la ligne 22 de mon fichier bdd.cs qui concerne ce bout de code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    public void Dispose()
            {
                Connection.Close();
                Command.Dispose();
                Connection.Dispose();
            }
    Voici mon code complet :

    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
    internal sealed class DatabaseClient : IDisposable
        {
            private DatabaseManager Manager;
            private SqlCommand Command;
            private SqlConnection Connection;
            public DatabaseClient(DatabaseManager _Manager)
            {
     
                Manager = _Manager;
                SqlConnection Connection = new SqlConnection(_Manager.ConnectionString);
               Command = Connection.CreateCommand();
                Connection.Open();
            }
            public void Dispose()
            {
                Connection.Close();
                Command.Dispose();
                Connection.Dispose();
            }
            public void AddParamWithValue(string sParam, object val)
            {
                Command.Parameters.AddWithValue(sParam, val);
            }
            public void ExecuteQuery(string sQuery)
            {
                Command.CommandText = sQuery;
                Command.ExecuteScalar();
                Command.CommandText = null;
            }
            public DataSet ReadDataSet(string Query)
            {
                DataSet dataSet = new DataSet();
                Command.CommandText = Query;
               using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(this.Command.ToString(), Connection))
     
            //  using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(this.Command))
                {
                    SqlDataAdapter.Fill(dataSet);
                }
                Command.CommandText = null;
                return dataSet;
            }
            public DataTable ReadDataTable(string Query)
            {
     
                DataTable dataTable = new DataTable();
                Command.CommandText = Query;
                using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(this.Command.ToString(), Connection))
                {
                    SqlDataAdapter.Fill(dataTable);
                }
                Command.CommandText = null;
                return dataTable;
            }
            public DataRow ReadDataRow(string Query)
            {
                DataTable dataTable = this.ReadDataTable(Query);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    return dataTable.Rows[0];
                }
                return null;
            }
            public string ReadString(string Query)
            {
                Command.CommandText = Query;
                string result = this.Command.ExecuteScalar().ToString();
                Command.CommandText = null;
                return result;
            }
            public int ReadInt32(string Query)
            {
                Command.CommandText = Query;
                int result = int.Parse(this.Command.ExecuteScalar().ToString());
                Command.CommandText = null;
                return result;
            }
            public uint ReadUInt32(string Query)
            {
                Command.CommandText = Query;
                uint result = (uint)this.Command.ExecuteScalar();
                Command.CommandText = null;
                return result;
            }
        }
    Je ne comprend pas trop comment régler ce soucis donc si vous pouviez m'éclairer ou m'aider à déboguer mon code.. afin qu'il exécute les requête..

  2. #2
    Membre éprouvé Avatar de Momoth
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2013
    Messages
    318
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2013
    Messages : 318
    Points : 1 236
    Points
    1 236
    Par défaut
    Bonjour,

    Peux tu mettre un point d'arrêt sur cette ligne pour voir si l'objet est null ou non, et voir si il est null pourquoi il l'est (Pas à pas détaillé ou d'autres points d'arrêts par exemple).

    Bonne soirée a tous.

    -- Momoth --
    La Triforce du développement : Fainéantise, Curiosité et Imagination.

  3. #3
    Futur Membre du Club
    Femme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Mars 2014
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 28
    Localisation : France

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2014
    Messages : 9
    Points : 8
    Points
    8
    Par défaut
    Citation Envoyé par Momoth Voir le message
    Bonjour,

    Peux tu mettre un point d'arrêt sur cette ligne pour voir si l'objet est null ou non, et voir si il est null pourquoi il l'est (Pas à pas détaillé ou d'autres points d'arrêts par exemple).

    Bonne soirée a tous.

    -- Momoth --
    Merci à toi d'avoir répondu j'ai résolu le soucis mais désormais quand la requête se lance mon application affiche
    Impossible de trouver le serveur 'System' dans sysservers. vérifiez le nom du serveur correct a été spécifié, executez la procédure stockée sp_addlinkerserver pour ajouter ce serveur à sysservers
    j'ai donc lancer un exec sp_addlinkedserver
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    exec sp_addlinkedserver 'ip du serveur';
    exec sp_addlinkedsrvlogin 'ip du serveur'
        , 'FALSE', NULL, 'Canterlot', 'mdp';
    mais sans succès il continue de me faire cette erreur j'ai bien vérifier ma chaine de connexion pourtant.

    mon application lance une exception sur cette ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     public DataTable ReadDataTable(string Query)
    		{
     
    			DataTable dataTable = new DataTable();
                Command.CommandText = Query;
                using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(this.Command.ToString(), Connection))
    			{
                    SqlDataAdapter.Fill(dataTable); // un problème sur cette ligne
    			}
    			Command.CommandText = null;
    			return dataTable;
    		}
    vous pouvez voir le code entier dans le tout premier message ..

  4. #4
    Membre expérimenté Avatar de callo
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Février 2004
    Messages
    887
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Togo

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Service public

    Informations forums :
    Inscription : Février 2004
    Messages : 887
    Points : 1 699
    Points
    1 699
    Par défaut
    Citation Envoyé par Canterlot Voir le message
    mais sans succès il continue de me faire cette erreur j'ai bien vérifier ma chaine de connexion pourtant.

    mon application lance une exception sur cette ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     public DataTable ReadDataTable(string Query)
    		{
     
    			DataTable dataTable = new DataTable();
                Command.CommandText = Query;
                using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(this.Command.ToString(), Connection))
    			{
                    SqlDataAdapter.Fill(dataTable); // un problème sur cette ligne
    			}
    			Command.CommandText = null;
    			return dataTable;
    		}
    vous pouvez voir le code entier dans le tout premier message ..
    A mon avis, il y a un problème au niveau de cette instruction:
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(this.Command.ToString(), Connection))
    {
        //....
    }
    Il faut passer soit l'objet command même (sans faire un ToStrin()) ou carrément, passer la commande sql à exécuter:
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(Query, Connection))
    {
        //....
    }
    ou
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    //Instancier un objet Commande
    var myCommand = new SqlCommand(Query);
    myCommand.Connection= Connection ;
    using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(myCommand))
    {
        //....
    }
    N'oubliez pas le tag et

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Juin 2010
    Messages
    210
    Détails du profil
    Informations personnelles :
    Localisation : France, Yvelines (Île de France)

    Informations forums :
    Inscription : Juin 2010
    Messages : 210
    Points : 243
    Points
    243
    Par défaut
    Bonjour,

    Command.CommandText = Query;
    using (SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(this.Command.ToString(), Connection))
    {
    SqlDataAdapter.Fill(dataTable); // un problème sur cette ligne
    }
    Pourquoi mettre this.Command.ToString() et ne pas utiliser "Query" ou encore Command.CommandText dans le new SqlDataAdapter ?
    Je pense que cela serait plus adapté.

    Et petite suggestion personnel :

    SqlDataAdapter SqlDataAdapter
    Je te conseille de nommer avec un signe distinctif tes variables en rajoutant par exemple un préfixe devant :
    SqlDataAdapter MySqlDataAdapter , cela te permettra d'y voir plus clair quand ton code prendra du volume.

    A+

Discussions similaires

  1. [NON RESOLU] Problème changement maitre détail - Requête SQL
    Par Leesox dans le forum Bases de données
    Réponses: 1
    Dernier message: 18/08/2005, 09h49
  2. Problème de date dans requête de màj imbriquée
    Par VirginieGE dans le forum Langage SQL
    Réponses: 11
    Dernier message: 20/07/2004, 15h34
  3. Problème de formulation de requète
    Par tellitocci dans le forum MS SQL Server
    Réponses: 4
    Dernier message: 26/06/2004, 05h05
  4. Problème DBExpress et sous requêtes ???
    Par Trulane dans le forum Bases de données
    Réponses: 5
    Dernier message: 26/03/2004, 14h40
  5. problème connexion à certaines applications ?
    Par Cornell dans le forum MS SQL Server
    Réponses: 12
    Dernier message: 27/02/2004, 17h55

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