public AuthorsList GetList() { AuthorsList authors = new AuthorsList(); SqlDataReader reader = null; try { SqlCommand command = _connection.CreateCommand(); command.CommandText = "SELECT * FROM Authors"; reader = command.ExecuteReader(); while (reader.Read()) { authors.Add(new Author((String)reader["FirstName"], (String)reader["LastName"])); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ooops", MessageBoxButton.OK); } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } return(authors); }
private void DataGrid_Loaded() { DBClass.openConnection(); DBClass.sql = "select * from authors"; DBClass.cmd.CommandType = CommandType.Text; DBClass.cmd.CommandText = DBClass.sql; DBClass.da = new SqlDataAdapter(DBClass.cmd); DBClass.dt = new DataTable(); DBClass.da.Fill(DBClass.dt); // wyciągamy dane int i = 0; int j = 0; AuthorsList.Clear(); using (SqlDataReader reader = DBClass.cmd.ExecuteReader()) { while (reader.Read()) { for (j = 0; j <= reader.FieldCount - 1; j++) // Looping throw colums { data[j] = reader.GetValue(j).ToString(); } AuthorsList.Add(new AuthorModel { Id = Int32.Parse(data[0]), FirstNameAuthor = data[1], LastNameAuthor = data[2] }); } } DBClass.closeConnection(); try { } catch (Exception exp) { MessageBox.Show(exp.Message); } }