O
O
Oleg Gutsulyak2018-05-06 12:39:20
WPF
Oleg Gutsulyak, 2018-05-06 12:39:20

How to write data to table from MySQL database?

How to display data from MySql database to a table?
here is what i get

Employee tabl = new Employee();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            fillGrid();

        }

        public void fillGrid() {
            MysqlConn Conn = new MysqlConn();
            MySqlDataReader reader = Conn.ConnectionDataBase(Con.RetSet("Host"), Con.RetSet("User"), Con.RetSet("DB"), Con.RetSet("Pass"), "SELECT * FROM s");

            while (reader.Read())
            {


                tabl.EmployeeId = reader["id"].ToString();
                tabl.EmployeePib = reader["pib"].ToString();
                tabl.EmployeeEmail = reader["email"].ToString();
                tabl.EmployeePhone = reader["phone"].ToString();
                tabl.EmployeeMessage = reader["message"].ToString();
                tabl.EmployeeIp = reader["ip"].ToString();
                tabl.EmployeeDate = reader["send"].ToString();
                
                tables.Items.Add(tabl);
            }

            // label1.Text = reader[3].ToString() + " " + reader[4].ToString();
            //this.Text = label1.Text;
            Conn.Cls();
        }
        public class Employee
        {
            public string EmployeePhone { get; set; }
            public string EmployeeId { get; set; }
            public string EmployeePib { get; set; }
            public string EmployeeEmail { get; set; }
            public string EmployeeMessage { get; set; }
            public string EmployeeIp { get; set; }
            public string EmployeeDate { get; set; }

        }

<DataGrid x:Name="tables" Margin="0 8 0 0" ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"
                          materialDesign:DataGridAssist.CellPadding="13 8 8 8" materialDesign:DataGridAssist.ColumnHeaderPadding="8">
                  
                    <DataGrid.Columns>

                        <DataGridTextColumn Width="35" Binding="{Binding EmployeeId}"
                                        Header="#"
                                        EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" />
                        <!-- if you want to use the pop up style (MaterialDesignDataGridTextColumnPopupEditingStyle), you must use MaterialDataGridTextColumn -->
                        <materialDesign:MaterialDataGridTextColumn Width="*" Binding="{Binding EmployeePib}"
                                                               Header="ПІБ"
                                                               EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}" 
                                                               />
                        <materialDesign:MaterialDataGridTextColumn Width="*" Binding="{Binding EmployeeEmail}"
                                                               Header="E-mail"
                                                               MaxLength="255" 
                                                               EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"  />

                        <materialDesign:MaterialDataGridTextColumn Width="*" Binding="{Binding EmployeeMessage}"
                                                               Header="Message"
                                                               MaxLength="255" 
                                                               EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"  />
                        <materialDesign:MaterialDataGridTextColumn Width="*" Binding="{Binding EmployeePhone}"
                                                               Header="Телефон"
                                                               MaxLength="255" 
                                                               EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"  />

                        <materialDesign:MaterialDataGridTextColumn Width="*" Binding="{Binding EmployeeIp}"
                                                               Header="IP"
                                                               MaxLength="255" 
                                                               EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"  />

                        <materialDesign:MaterialDataGridTextColumn Width="*" Binding="{Binding EmployeeDate}"
                                                               Header="Дата"
                                                               MaxLength="255" 
                                                               EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"  />



                    </DataGrid.Columns>
                </DataGrid>

5aeece285a96a800657058.png
phpmyadmin
5aeecd3a25e83829451073.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ter, 2018-05-06
@Bender100

Probably something like this:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tables.ItemsSource = fillGrid();
        }

    public class Employee
        {
            public string EmployeePhone { get; set; }
            public string EmployeeId { get; set; }
            public string EmployeePib { get; set; }
            public string EmployeeEmail { get; set; }
            public string EmployeeMessage { get; set; }
            public string EmployeeIp { get; set; }
            public string EmployeeDate { get; set; }
        }
    
        public List<Employee> fillGrid() {
            MysqlConn Conn = new MysqlConn();
            MySqlDataReader reader = Conn.ConnectionDataBase(Con.RetSet("Host"), Con.RetSet("User"), Con.RetSet("DB"), Con.RetSet("Pass"), "SELECT * FROM s");

      var _list = new List<Employee>();
      
            while (reader.Read())
            {
        var tabl = new Employee()
        {
          EmployeeId = reader["id"].ToString(),
          EmployeePib = reader["pib"].ToString(),
          EmployeeEmail = reader["email"].ToString(),
          EmployeePhone = reader["phone"].ToString(),
          EmployeeMessage = reader["message"].ToString(),
          EmployeeIp = reader["ip"].ToString(),
          EmployeeDate = reader["send"].ToString()				
        };
              
                _list.Add(tabl);
            }

            Conn.Cls();
      
      return _list;
        }

I would still look towards an ORM like Dapper and MVVM

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question