How to obtain data from Banco Mysql?

0

I have this code snippet where it takes the information from a database field and passes it to the textBlock, so now I need to populate some textblock.

  

EX: In the database has name = maria Rg = 123 cpf = 456 ai in the program vai   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"));
                }

            }

        }
    
asked by anonymous 09.03.2017 / 13:10

1 answer

1

If this is what I understand, just increment the fields in the query and in the 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 "));
        }
    }
}
    
09.03.2017 / 13:17