A
A
Anton_repr2019-10-25 13:33:18
MySQL
Anton_repr, 2019-10-25 13:33:18

How to display table from database to datagrid?

Connecting to the database is done like this:

public static MySqlConnection connection = new MySqlConnection();
        public static MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();

        public static void EstablishConnection()
        {
                builder.Server = "***.***.*.**";
                builder.UserID = "student";
                builder.Password = "student";
                builder.Database = "recipesbook";              
                connection = new MySqlConnection(builder.ToString());            
        }

Next, I'm trying to write 3 columns to the datagrid:
string sql = "SELECT name, amount, time from ListOfProducts";
            using(MySqlConnection connection = new MySqlConnection(SQLconnect.builder.ToString()))
            {
                connection.Open();
                using(MySqlCommand command = new MySqlCommand(sql, connection))             
                    using (MySqlDataReader dataReader = command.ExecuteReader())
                    {
                        if(dataReader.HasRows)
                        {
                            while(dataReader.Read())
                            {
                                name = dataReader.GetString(0);
                                amount = dataReader.GetInt32(1);
                                time = dataReader.GetInt32(2);                              
                            }
                        }
                        else
                        {
                            MessageBox.Show("no rows");
                        }
                    }
                connection.Close();

How to display it in datagrid?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question