Partage
  • Partager sur Facebook
  • Partager sur Twitter

System.Data.SqlClient.SqlException

erreur de synthaxe dans le code sql que je n'arrive par a corriger

Sujet résolu
    9 mai 2019 à 16:24:21

    private DataView GetFournisseur(DateTime dateDebut, DateTime dateFin)
            {
                DataView view = null;
                DataTable table = null;
                StringBuilder commandText = new StringBuilder();
    
                commandText.Append("SELECT ");
                commandText.Append("'' AS [ClientFournisseur], ");
                commandText.Append("Client.ID AS [ClientID], ");
                commandText.Append("Client.Code AS [ClientCode], ");
                commandText.Append("Client.Nom AS [ClientNom], ");
                commandText.Append("Fournisseur.ID AS [FournisseurID], ");
                commandText.Append("Fournisseur.Code AS [FournisseurCode], ");
                commandText.Append("Fournisseur.Descr AS [FournisseurDescr], ");
                commandText.Append("Fournisseur.Descr AS [FournisseurDescrHtml], ");
                commandText.Append("Fournisseur.Encours AS [FournisseurEncours], ");
                commandText.Append("Fournisseur.pStatutClientFournisseurID AS [FournisseurStatut], ");// n
                commandText.Append("Statut.Libelle AS [FournisseurStatutLibelle], ");// n
                commandText.Append("Statut.ID AS [FournisseurStatutID], ");// n
                commandText.Append("'' AS [Statut], ");
                commandText.Append("Fournisseur.DateOuverture AS [FournisseurDateOuverture], ");
                commandText.Append("Fournisseur.DateFermeture AS [FournisseurDateFermeture], ");
                commandText.Append("FournisseurAgentResponsable.ID AS [FournisseurAgentResponsableID], ");
                commandText.Append("LTRIM(FournisseurAgentResponsable.Code) AS [FournisseurAgentResponsableCode], ");
                commandText.Append("LTRIM(FournisseurAgentResponsable.Nom) AS [FournisseurAgentResponsableNom], ");
                commandText.Append("FournisseurAgentRespFact.ID AS [FournisseurAgentRespFactID], ");
                commandText.Append("LTRIM(FournisseurAgentRespFact.Code) AS [FournisseurAgentRespFactCode], ");
                commandText.Append("LTRIM(FournisseurAgentRespFact.Nom) AS [FournisseurAgentRespFactNom], ");
                commandText.Append("FournisseurAgentSource.ID AS [FournisseurAgentSourceID], ");
                commandText.Append("LTRIM(FournisseurAgentSource.Code) AS [FournisseurAgentSourceCode], ");
                commandText.Append("LTRIM(FournisseurAgentSource.Nom) AS [FournisseurAgentSourceNom], ");
                commandText.Append("'' AS [Contact], ");
                commandText.Append("ISNULL(Contact.IsCompany, '') AS [ContactIsCompany], ");
                commandText.Append("ISNULL(ContactIntroduction.IntroductionCourte, '') AS [ContactIntroduction], ");
                commandText.Append("ISNULL(Contact.FirstName, '') AS [ContactFirstName], ");
                commandText.Append("ISNULL(Contact.MiddleName, '') AS [ContactMiddleName], ");
                commandText.Append("ISNULL(Contact.LastName, '') AS [ContactLastName], ");
                commandText.Append("ISNULL(Contact.CompanyName, '') AS [ContactCompanyName] ");
                commandText.Append("ISNULL((");
                commandText.Append("SELECT Information.Valeur");
                commandText.Append("FROM tblFournisseur");
                commandText.Append("INNER JOIN Information ON tblFournisseur.ID = Information.pValeurID AND Information.NoTable = 14");
                commandText.Append("WHERE tblFournisseur.ID = Fournisseur.ID");
                commandText.Append("AND Information.Numero = 1");
                commandText.Append("), '') AS [DateFourniture]");
                commandText.Append("ISNULL('', '') AS [DateFourniture]");//
    
                commandText.Append("FROM tblFournisseur Fournisseur ");
                commandText.Append("INNER JOIN tblClient Client ON Fournisseur.ClientID = Client.ID ");
                commandText.Append("INNER JOIN tblAgent FournisseurAgentResponsable ON Fournisseur.AgentResponsableID = FournisseurAgentResponsable.ID ");
                commandText.Append("INNER JOIN tblAgent FournisseurAgentRespFact ON Fournisseur.AgentRespFactID = FournisseurAgentRespFact.ID ");
                commandText.Append("INNER JOIN tblAgent FournisseurAgentSource ON Fournisseur.pAgentSourceID = FournisseurAgentSource.ID ");
                commandText.Append("LEFT OUTER JOIN tblMMFournisseurContacts FournisseurContact ON Fournisseur.ID = FournisseurContact.pFournisseurID AND FournisseurContact.pMMRoleID = 8002 ");
                commandText.Append("LEFT OUTER JOIN tblMMContacts Contact ON FournisseurContact.pMMContactID = Contact.ID ");
                commandText.Append("LEFT OUTER JOIN tblIntroduction ContactIntroduction ON Contact.pPrefixID = ContactIntroduction.ID ");
                commandText.Append("LEFT OUTER JOIN tblStatutClientFournisseur Statut ON Fournisseur.pStatutClientFournisseurID = Statut.ID ");// n
    
    
                commandText.Append("WHERE Fournisseur.ID <> 0 ");
                commandText.Append("AND Fournisseur.Encours = 1 ");
                commandText.AppendFormat("AND Fournisseur.DateOuverture >= '{0}' ", String.Format(CultureInfo.InvariantCulture, "{0:yyyy-MM-dd 00:00:00}", dateDebut));
                commandText.AppendFormat("AND Fournisseur.DateOuverture <= '{0}' ", String.Format(CultureInfo.InvariantCulture, "{0:yyyy-MM-dd HH:mm:ss}", dateFin));
    
                Log.Information(commandText.ToString());
    
                table = this._userDatabase.ExecuteDataSet(CommandType.Text, commandText.ToString()).Tables[0];
    
                //table.Columns.Add("ClientFournisseur");
                //table.Columns.Add("Statut");
                //table.Columns.Add("Contact");
    
                foreach (DataRow row in table.Rows)
                {
                    string clientCode = Tools.GetString(row["ClientCode"]).Trim();
                    string FournisseurCode = Tools.GetString(row["FournisseurCode"]).Trim();
                    string FournisseurDescr = Tools.GetString(row["FournisseurDescr"]).Trim();
                    int FournisseurEncours = Tools.GetInteger(row["FournisseurEncours"]);
                    int FournisseurStatut = Tools.GetInteger(row["FournisseurStatut"]); //
                    int FournisseurStatutID = Tools.GetInteger(row["FournisseurStatutID"]); //
                    int contactIsCompany = Tools.GetInteger(row["ContactIsCompany"]);
                    string contactIntroduction = Tools.GetString(row["ContactIntroduction"]).Trim();
                    string contactFirstName = Tools.GetString(row["ContactFirstName"]).Trim();
                    string contactMiddleName = Tools.GetString(row["ContactMiddleName"]).Trim();
                    string contactLastName = Tools.GetString(row["ContactLastName"]).Trim();
                    string contactCompanyName = Tools.GetString(row["ContactCompanyName"]).Trim();
                    string FournisseurStatutLibelle = Tools.GetString(row["FournisseurStatutLibelle"]).Trim();// n
                    
                    //
                    // Client/Fournisseur
                    //
                    row["ClientFournisseur"] = String.Format("{0}/{1}", clientCode.Trim(), FournisseurCode.Trim());
    
                    //
                    // Statut
                    //
                    //if (FournisseurEncours == 1)
                    //{
                    //    row["Statut"] = "Encours";
                    //}
                    //else
                    //{
                    //    row["Statut"] = "Terminé";
                    //}
                    //
                    // Statut  
                    //
                    if (FournisseurStatut == FournisseurStatutID)
                    {
                        //FournisseurStatut == 1;
                        row["Statut"] = String.Format("{0}", FournisseurStatutLibelle.Trim()); //
                    }
                    //
                    // Contact
                    //
                    StringBuilder contact = new StringBuilder();
                    if (contactIsCompany == 1)
                    {
                        row["Contact"] = contactCompanyName;
                    }
                    else
                    {
                        if (contactIntroduction.Trim().Length > 0)
                        {
                            contact.Append(contactIntroduction);
                        }
    
                        if (contactFirstName.Trim().Length > 0)
                        {
                            if (contact.Length > 0)
                            {
                                contact.Append(" ");
                            }
                            contact.Append(contactFirstName);
                        }
    
                        if (contactMiddleName.Trim().Length > 0)
                        {
                            if (contact.Length > 0)
                            {
                                contact.Append(" ");
                            }
                            contact.Append(contactMiddleName);
                        }
    
                        if (contactLastName.Trim().Length > 0)
                        {
                            if (contact.Length > 0)
                            {
                                contact.Append(" ");
                            }
                            contact.Append(contactLastName);
                        }
                    }
    
                    row["Contact"] = contact.ToString();
                }
    
                view = table.DefaultView;
                view.Sort = "FournisseurDateOuverture";
    
                return view;
            }


    depuis hier j'essaie de cooririger cette erreur.

    System.Data.SqlClient.SqlException : 'Incorrect syntax near 'ISNULL'.
    Incorrect syntax near the keyword 'JOIN'.'

    de sont des des codes que j'ai trouve a ma nouvelle structure

    voici la requete

    • Partager sur Facebook
    • Partager sur Twitter
      10 mai 2019 à 9:21:32

      Bonjour,

      Le problème avec le StringBuilder() est qu'il va construire ton string comme tu lui renvoies ... donc pour l'erreur du join, il va construire le string n'importe comment vu que tu as oublié un espace :)

      commandText.Append("FROM tblFournisseur");
      commandText.Append("INNER JOIN Information ON tblFournisseur.ID = Information.pValeurID AND Information.NoTable = 14");
      
      //Ce qui donne
      //FROM tblFournisseurINNER JOIN Information ON tblFournisseur.ID = Information.pValeurID AND Information.NoTable = 14


      Pour l'autre erreur, tu devrais mettre un point d'arrêt à la fin de la construction du string et le récupérer. Tu peux le tester dans ta solution de base de données pour voir où est l'erreur, ça sera plus simple.

      o/

      • Partager sur Facebook
      • Partager sur Twitter
      Insanity is doing the exact same fuckin' thing over and over again, expecting shit to change...
        10 mai 2019 à 10:33:33

        @Nairowyn, il log déjà la valeur de son StringBuilder dans son code, il n'a qu'à utiliser directement sa trace dans un client SQL de son gestionnaire de base de données pour voir les erreurs SQL.

        Mais cette manière de faire des requêtes SQL est clairement archaïque et dangereuse, via de la SQL Injection.

        Au minimum, vous devriez utiliser @ (verbatim string) pour avoir un code SQL plus claire

        https://stackoverflow.com/questions/1100260/multiline-string-literal-in-c-sharp

        et faire des requêtes paramétrées :

        https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements

        Utilisez aussi correctement la paire DataSet/DataAdapter plutôt que cette antiquité de DataReader ou pire, comme ici, utiliser un DataSet venant d'une DAL comme un DataReader. (des colonnes calculées dans une DataTable de DataSet, ça existe, etc...)

        Pour aller plus loin et simplifier votre usine à gaz, LINQ to SQL et/ou Entity Framework réduiraient votre code à quelques lignes maximum.

        • Partager sur Facebook
        • Partager sur Twitter
        Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.

        System.Data.SqlClient.SqlException

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown