private void Load(string table_name, string sql_query) { ColumnNames = new List <string>(); Rows = new List <DatabaseRow>(); TableName = table_name; SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); // Grab the conection string for accessing the database. SqlCommand command = new SqlCommand(sql_query, connection); // Associate the given sql query with the connection. connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Send the sql query to the database for processing. for (int n = 0; n < reader.FieldCount; ++n) { ColumnNames.Add(reader.GetName(n)); } while (reader.Read()) // Access each student in turn (continue until there are no students left to read). { DatabaseRow dr = new DatabaseRow(); for (int column = 0; column < ColumnCount; ++column) { dr.Add(ColumnNames[column], reader[column].ToString()); } Rows.Add(dr); } connection.Close(); }
/// <summary> /// Returns a new/blank DatabaseRow with same columns as the owner table. /// </summary> /// <returns>DatabaseRow</returns> public DatabaseRow NewRow() { DatabaseRow dr = new DatabaseRow(); for (int column = 0; column < ColumnCount; ++column) { dr.Add(ColumnNames[column], ""); } return(dr); }