Alexandre Hansen
Alexandre Hansen

Reputation: 61

Read Datatable and do a loop in rows async c#

I'm trying to implement asynchronous methods in my program, and I want to read each row asynchronously from a datatable. I have the following situation:

   private void VerifyPermissions()
    {
        try
        {
            string constring = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", AcessoBancoDados.server, AcessoBancoDados.user, AcessoBancoDados.password, AcessoBancoDados.database);

            MySqlConnection con = new MySqlConnection();
            con.ConnectionString = constring;
            con.Open();

            var query = "SELECT id FROM users";

            MySqlCommand cmd = new MySqlCommand(query, con);
            MySqlDataAdapter da = new MySqlDataAdapter(query, con);

            DataTable dt = new DataTable();

            da.Fill(dt);
            con.Close();

            foreach (DataRow item in dt.Rows)
            {
               Messagebox.Show(item["id"].ToString());
            }
    }

And the call method:

private void button1_Click(object sender, EventArgs e)
    {
       VerifyPermissions()
    }

Anyone can show me an async situation for this? Thanks.

Upvotes: 1

Views: 1562

Answers (2)

gsharp
gsharp

Reputation: 27937

according to your comment you would need something like my code below. If you use SqlCommand instead of SqlDataAdapter you will have async methods already and don't need to create a task.

(also don't mix ui and data access. keep them separate.)

    private async void button1_Click(object sender, EventArgs e)
    {
        await VerificarPermissoes();
    }


    private async Task VerificarPermissoes()
    {
        await Task.Run(() =>
        {
              // put your code from above here.
        }); 
    }

Upvotes: 1

Bruno Warmling
Bruno Warmling

Reputation: 400

Well, you can do this basically by creating tasks in c#, see the example below

private void VerificarPermissoes()
    {
        try
        {
            DataTable dt = new DataTable();
            string constring = String.Format("");
            string query = "SELECT id FROM users";

            using (SqlConnection con = new SqlConnection(constring))
            {
                con.Open();

                DbCommand command = con.CreateCommand();
                command.CommandText = query;
                dt.Load(command.ExecuteReader());
            }

            foreach (DataRow item in dt.Rows)
            {
                Task.Factory.StartNew(delegate () { ProcessItem(item["id"].ToString()); });
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void ProcessItem(string item)
    {
        AddControl(myControl, childControl);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        VerificarPermissoes();
    }



    private void AddControl(Control ctrl, Control child)
        {
            if (ctrl.InvokeRequired)
            {
                Action act = delegate () { AddControl(ctrl, child); };
                this.Invoke(act);
            }
            else
            {
                ctrl.Controls.Add(child);
            }
        }

Upvotes: 0

Related Questions