How to get data from Mysql Database?

Asked

Viewed 176 times

0

I have this code snippet where it takes the information from a database field and goes to textBlock, but now I need to fill in some textblock.

EX: In the database has name=maria Rg=123 Cpf=456 ai in the program goes have the textblock that will take name, Rg, Cpf.

Code:

 using (_connection = new MySqlConnection("Database=test;Data Source=localhost;User Id=root;Password=teste;SslMode=None;"))
        {
            System.Text.EncodingProvider ppp;
            ppp = System.Text.CodePagesEncodingProvider.Instance;
            Encoding.RegisterProvider(ppp);

            _connection.Open();
            var cmd = new MySqlCommand("select name from user where id=1", _connection);
            using (var reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    textBlock.Text = (reader.GetString("name"));
                }

            }

        }
  • you that put more these fields ? in query and textBlock

  • I have in the database the name field that is assumed by textblock 1. I now added the fields Rg and CPF in the database and I want it to be assumed by textblock 2 and 3.

1 answer

2


If that’s what I understand, just increment the fields in the query and text.

using (_connection = new MySqlConnection("Database=test;Data Source=localhost;User Id=root;Password=teste;SslMode=None;"))
{
    System.Text.EncodingProvider ppp;
    ppp = System.Text.CodePagesEncodingProvider.Instance;
    Encoding.RegisterProvider(ppp);

    _connection.Open();
    var cmd = new MySqlCommand("select nome, Rg, Cpf from user where id=1", _connection);
    using (var reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            textBlocknome.Text = (reader.GetString("name"));
            textBlockRg.Text = (reader.GetString("Rg"));
            textBlockCpf .Text = (reader.GetString("Cpf "));
        }
    }
}

Browser other questions tagged

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