How to store multiple user names in one variable?

Asked

Viewed 253 times

1

I have a form, and in it I make a query in the database:

private void Consulta()
{
  var dataCom = Convert.ToDateTime(label1.Text).ToShortDateString(); 

  var conf = $"select * from data where data_venc = '{Datacom}'";
  var comando = new MySqlCommand(conf, conn);

  conn.Open();

  var retorno = Convert.ToInt32(comando.ExecuteScalar());

  conn.Close();
}

The variable Datacom receives the current system date.

In the database I have two dates (registration date and due date), when registering user, add 30 days and save as due date.

I want to check if the due date is equal to the system, if it is, save the names of users in a variable (or whatever the type) so I can use later.

After storing these names of these variables, I want to take each name to send emails each with your name.

How can I do that?

2 answers

1

The way your code is, it will not return the database records, only the total of records affected (ExecuteScalar).
To return the records, and then scroll through them, you will need to use the command ExecuteReader.

As you want to store in a variable, just use it.
I modified your method to make it possible to return your records query:

private MySqlDataReader Consulta(string connectionString) {
    MySqlDataReader retorno;

    using (var conn = new MySqlConnection(connectionString)) {


        var dataCom = Convert.ToDateTime(label1.Text).ToShortDateString();

        var conf = "select * from data where data_venc = @dataCom";
        conn.Open();

        using (var comando = new MySqlCommand(conf, conn)) {
            comando.Parameters.AddWithValue("@dataCom", dataCom);

            retorno = comando.ExecuteReader();
        }
    }
    return retorno;
}

With this method, you can then create a loop to go through the records, and with this, send the emails as you wish.

while (Consulta("sua_string_conexao").Read()) {
    //Percorre o datareader - cara interação será um usuario que poderá enviar o email

}

Recommended readings:

It is correct to use a block using within another block using?
Datareader blocks using Datareader "Close()"?

  • good, but if I store several names in a variable I can utilazar these names separately to send an email to each names of those separated?

  • @Juniorquaresmagutekoski, yes you just create a List<T> or Array, in the case you prefer. Then go through this created object which in this case is a list or an array and do the individual processing for each name that is on that object. Then just fire the e-mails

  • @Juniorquaresmagutekoski Hello, consider accepting my answer if it has been useful to you. If you think she’s incomplete or doesn’t respond to you, make the appropriate comments so I can improve her.

0

I created an Array of user objects and add users that are expired to it.

Then send this array to the function that will send the e-mail.


Straight to your question.

 ArrayList nomesExpirados = new ArrayList();

 nomesExpirados.add("nome cliente 1")

 nomesExpirados.add("nome cliente 2")

then just send this array to the function of sending email.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.