MySQL: Fehlerhafte Suchanfrage?

Hallo,

ich stehe vor einer Herausforderung mit einer Windows Forms-Anwendung, die ich entwickelt habe, um Tageszeitraumstatistiken für Benutzer anzuzeigen. In dieser Anwendung gibt es ein Formular mit dem Namen TagesZeitraumStatistik, das darauf ausgelegt ist, Benutzern ihre Umsätze basierend auf ihnen zugewiesenen PIDs (Produkt-IDs) anzuzeigen.

Jedoch stieß ich auf ein Problem: Das Programm lädt nicht alle PIDs korrekt. Insbesondere scheinen einige Unter-PIDs übersprungen zu werden.

Bei der Fehlersuche mittels Debugging und Setzen von Breakpoints konnte ich beobachten, dass initial alle PIDs korrekt erfasst werden. Allerdings überspringt das Programm im weiteren Verlauf bestimmte PIDs, ohne dass ein offensichtlicher Grund dafür erkennbar ist. Leider komme ich bei dem Problem nicht wirklich weiter. Vielleicht kann mir ja hier jemand helfen.

Hier die Codeausschnitte:

public TagesZeitraumStatistik(string username, bool isAdmin, List<string> userPIDs)
{
  InitializeComponent();
  InitializeDataGridView();
  FillYearComboBox();

  this.username = username;
  this.isAdmin = isAdmin;
  this.userPIDs = userPIDs;
  this.Load += new EventHandler(TagesZeitraumStatistik_Load);

  monthCalendar1.DateSelected += MonthCalendar1_DateSelected;
  comboBoxYear.SelectedIndexChanged += ComboBoxYear_SelectedIndexChanged;
  printDocument1.PrintPage += new PrintPageEventHandler(PrintDocument1_PrintPage);

  // TextBox für den PID-Filter erstellen und konfigurieren
  textBoxPidFilter = new TextBox();
  textBoxPidFilter.Location = new Point(10, 10);
  textBoxPidFilter.Size = new Size(150, 20);
  this.Controls.Add(textBoxPidFilter);

  // LoadData-Methode aufrufen, um die Daten zu laden
  LoadData();
}

private void LoadData(DateTime? startDate = null, DateTime? endDate = null, string pidStartsWith = null)
{
  try
  {
    List<string> pidFilterList = new List<string>();

    if (!string.IsNullOrEmpty(pidStartsWith))
    {
      pidFilterList.Add(pidStartsWith);
    }

    DataTable statistikData = dbManager.GetTageszeitraumStatistikData(startDate, endDate, pidFilterList, userPIDs);
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = statistikData;
    UpdateTotalSales(statistikData);
    UpdateSalesLabel(startDate, endDate);
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}

public DataTable GetTageszeitraumStatistikData(DateTime? startDate = null, DateTime? endDate = null, List<string> pidPrefixes = null, List<string> userPids = null)
{
  DataTable dt = new DataTable();

  using (MySqlConnection conn = new MySqlConnection(connectionString))
  {
    try
    {
      conn.Open();
      string query = @"
        SELECT Datum, FreierText1 AS PID, Name AS Nachname, EuroNetto, Belegnummer
        FROM BELEG 
        WHERE 1=1";

      if (startDate.HasValue)
      {
        query += " AND Datum >= @StartDate";
      }

      if (endDate.HasValue)
      {
        query += " AND Datum <= @EndDate";
      }

      if (pidPrefixes != null && pidPrefixes.Any())
      {
        query += " AND (";

        for (int i = 0; i < pidPrefixes.Count; i++)
        {
          query += $"FreierText1 LIKE @Pid{i}";

          if (i < pidPrefixes.Count - 1)
          {
            query += " OR ";
          }
        }

        query += ")";
      }

      if (userPids != null && userPids.Count > 0)
      {
        string pidsCondition = string.Join(",", userPids.Select(pid => $"'{pid}'"));
        query += $" AND FreierText1 IN ({pidsCondition})";
      }

      using (MySqlCommand cmd = new MySqlCommand(query, conn))
      {
        if (startDate.HasValue)
        {
          cmd.Parameters.AddWithValue("@StartDate", startDate.Value.Date);
        }

        if (endDate.HasValue)
        {
          cmd.Parameters.AddWithValue("@EndDate", endDate.Value.Date);
        }

        if (pidPrefixes != null && pidPrefixes.Any())
        {
          for (int i = 0; i < pidPrefixes.Count; i++)
          {
            cmd.Parameters.AddWithValue($"@Pid{i}", $"{pidPrefixes[i]}%");
          }
        }

        using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
        {
          da.Fill(dt);
        }
      }
    }
    catch (MySqlException ex)
    {
      throw new Exception($"Fehler beim Abrufen der Daten: {ex.Message}", ex);
    }
  }

  return dt;
}
C Sharp, Code, Datenbank, MySQL, Programmiersprache, Visual Studio, Algorithmus
Statische Interface Member?

Hallo,

Ich habe mal einwenig im Code der .NET-Standart Bibliothek gestöbert und bin dabei auf dieses Interface gestoßen

//
// Zusammenfassung:
//     Defines a mechanism for parsing a string to a value.
//
// Typparameter:
//   TSelf:
//     The type that implements this interface.
public interface IParsable<TSelf> where TSelf : IParsable<TSelf>?
{
    //
    // Zusammenfassung:
    //     Parses a string into a value.
    //
    // Parameter:
    //   s:
    //     The string to parse.
    //
    //   provider:
    //     An object that provides culture-specific formatting information about s.
    //
    // Rückgabewerte:
    //     The result of parsing s.
    //
    // Ausnahmen:
    //   T:System.ArgumentNullException:
    //     s is null.
    //
    //   T:System.FormatException:
    //     s is not in the correct format.
    //
    //   T:System.OverflowException:
    //     s is not representable by TSelf.
    static abstract TSelf Parse(string s, IFormatProvider? provider);
    //
    // Zusammenfassung:
    //     Tries to parse a string into a value.
    //
    // Parameter:
    //   s:
    //     The string to parse.
    //
    //   provider:
    //     An object that provides culture-specific formatting information about s.
    //
    //   result:
    //     When this method returns, contains the result of successfully parsing s or an
    //     undefined value on failure.
    //
    // Rückgabewerte:
    //     true if s was successfully parsed; otherwise, false.
    static abstract bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TSelf result);
}

Kommt es nur mir so vor oder kommt es euch auch einfach falsch vor das ein Interface statische und abstrakte Member vorschreibt und dazu noch das der abstrakte Member statisch ist (Wenn ich versuche einen statischen Member einer Klasse als abstrakt zu deklarieren kommt ein Compilerfehler).

Falls jemand eine Idee hat wozu das gut sein kann, dann bitte eine Antwort schreiben.

C Sharp, dotNet, Programmiersprache

Meistgelesene Beiträge zum Thema C Sharp