Как записать даные в таблицу с базы Mysql?

как вывести данные с базы MySql в таблицу?
вот что у меня получаеться
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
  • Вопрос задан
  • 157 просмотров
Решения вопроса 1
alexsandr0000
@alexsandr0000
Программист C#/C++/C
Наверное, как-то так:

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;
        }


Я бы все же посмотрел в сторону ORM типа Dapper и MVVM
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы