Home > front end >  How do I display all the lines that contains the card number?
How do I display all the lines that contains the card number?

Time:12-01

Right now this is just presenting a line of the database but I need to present more, if anyone knew how to help me i would be grateful

private static string extratoOperacao(string numeroCartao)
{
    return getExtrato($@"SELECT CardNumber, Deposit, Withdraw, DataHora FROM MoveInfo WHERE CardNumber = '{numeroCartao}'");
}
private static string getExtrato(string query)
{
    using (var cn = new SqlConnection("Data Source=MAD-PC-023\\SQLEXPRESS;Database=bank;Trusted_Connection=True;"))
    {
        cn.Open();
        using (var cmd = new SqlCommand() { Connection = cn, CommandText = query })
        {
            var reader = cmd.ExecuteReader();
             while (reader.Read() == true)
            {
                    if (reader.GetString(1) == null)
                    {
                        return "\n  O cartão nº "   reader.GetString(0)   " levantou: "   reader.GetString(2)   " às: "   reader.GetDateTime(3);
                    }
                    else
                    {
                        return "\n  O cartão nº "   reader.GetString(0)   " depositou: "   reader.GetString(1)   " euros "   " às: "   reader.GetDateTime(3);
                    }
            }
            return "";
        }
    }
}

The supposed is to show all the information of the lines where the card number is equal to the inserted

CodePudding user response:

The return statement is going to exit your function, which is why you only get one result. If you want multiple lines, you're going to need to build and return a collection (e.g., array, list, etc.) or use yield return . . . the collection is probably the most straight-forward approach.

If you want all of the results in a single string, you'll need StringBuilder.

  • Related