How to store multiple user names in a variable?

1

I have a form , and 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 (date of registration and expiration date), when making the user registration, I add 30 days and saved as the due date.

I want to check if the due date is the same as the system, if so, save the user names in a variable (or whatever the type) so that I can use it later.

After saving these names of these variables, I want to get each name to send emails each with their name.

How can I do this?

    
asked by anonymous 19.04.2017 / 05:41

2 answers

1

As your code is, it will not return the records in the database, only the total of affected records (% with%). To return the logs, and then go through them, you will have to use the command ExecuteScalar .

How do you want to store in a variable, just use it. I have modified your method to either be able to return the records of your 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 that, 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:

Is it correct to use a using block within another using block?
DataReader in using blocks execute DataReader's" Close () "?

    
19.04.2017 / 15:03
0

I've created an Array of user objects and go adding the users that are expired to it.

Then send this array to the function that will send the email.

Direct your question.

 ArrayList nomesExpirados = new ArrayList();

 nomesExpirados.add("nome cliente 1")

 nomesExpirados.add("nome cliente 2")

Then just send this array names to the Send E-mail function.

    
19.04.2017 / 06:09