// fetch all rows from table airport_identifier into new List of class instances // links: // docLink: http://sql2x.org/documentationLink/7ca0c014-527e-4a0a-bd1f-12f4d8ea4b43 public static List <CrudeAirportIdentifierData> FetchAll() { var dataList = new List <CrudeAirportIdentifierData>(); // create query against airport_identifier // this will be ansi sql and parameterized // parameterized queries are a good way of preventing sql injection // and to make sure the query plan is pre-compiled // links: // docLink: http://sql2x.org/documentationLink/72f9f1bc-447c-4327-9d26-4b0790a07ff8 string sql = @" select airport_identifier_id, airport_id, airport_identifier_type_rcd, airport_identifier_code, user_id, date_time from [airport_identifier] order by airport_identifier_code"; // open standard connection // the connection is found in web.config // the connection is closed upon completion of the reader // links: // docLink: http://sql2x.org/documentationLink/952b3f82-bc00-4e82-9430-6bc26ff8bc4d using (var conn = new SqlConnection(Conn.ConnectionString)) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // execute query against airport_identifier // if the query fails in the preprocessor of sql server // an exception will be raised // links: // docLink: http://sql2x.org/documentationLink/ed55cc5b-d6be-4f5e-9385-ee726dfc2bf1 IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); // read all rows returned from the query of airport_identifier // read all columns from the datareader and // populate the List of C# objects with them // links: // docLink: http://sql2x.org/documentationLink/c1b8b800-b250-4822-a699-d93a35f4414d while (reader.Read()) { var data = new CrudeAirportIdentifierData(); data.Populate(reader); dataList.Add(data); } } return(dataList); } }
// fetch all from table into new List of class instances, filtered by any column // links: // docLink: http://sql2x.org/documentationLink/a736bbfd-030d-492e-a86a-7a5e478eeb79 public static List <CrudeAirportIdentifierData> FetchWithFilter(System.Guid airportIdentifierId, System.Guid airportId, string airportIdentifierTypeRcd, string airportIdentifierCode, System.Guid userId, System.DateTime dateTime) { var dataList = new List <CrudeAirportIdentifierData>(); // create query against airport_identifier // this will be ansi sql and parameterized // parameterized queries are a good way of preventing sql injection // and to make sure the query plan is pre-compiled // links: // docLink: http://sql2x.org/documentationLink/06da7a50-8760-48cd-b789-a41cac3edd13 string sql = @" select airport_identifier_id, airport_id, airport_identifier_type_rcd, airport_identifier_code, user_id, date_time from [airport_identifier] where 1 = 1"; // open standard connection // the connection is found in web.config // the connection is closed upon completion of the reader // links: // docLink: http://sql2x.org/documentationLink/6ec2495f-3a49-4a94-ad59-0ce064fc8654 using (var conn = new SqlConnection(Conn.ConnectionString)) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // add search column(s) if they are not null or empty // this search column(s) will be used together with the prepared ansi sql statement // links: // docLink: http://sql2x.org/documentationLink/2193123a-3534-412b-8521-dac14bb3884d if (airportIdentifierId != Guid.Empty) { sql += " and airport_identifier_id = @airport_identifier_id"; command.Parameters.Add("@airport_identifier_id", SqlDbType.UniqueIdentifier).Value = airportIdentifierId; } if (airportId != Guid.Empty) { sql += " and airport_id = @airport_id"; command.Parameters.Add("@airport_id", SqlDbType.UniqueIdentifier).Value = airportId; } if (!string.IsNullOrEmpty(airportIdentifierTypeRcd)) { sql += " and airport_identifier_type_rcd like '%' + @airport_identifier_type_rcd + '%'"; command.Parameters.Add("@airport_identifier_type_rcd", SqlDbType.NVarChar).Value = airportIdentifierTypeRcd.Replace("'", "''"); } if (!string.IsNullOrEmpty(airportIdentifierCode)) { sql += " and airport_identifier_code like '%' + @airport_identifier_code + '%'"; command.Parameters.Add("@airport_identifier_code", SqlDbType.NVarChar).Value = airportIdentifierCode.Replace("'", "''"); } if (userId != Guid.Empty) { sql += " and user_id = @user_id"; command.Parameters.Add("@user_id", SqlDbType.UniqueIdentifier).Value = userId; } if (dateTime != DateTime.MinValue) { sql += " and date_time = @date_time"; command.Parameters.Add("@date_time", SqlDbType.DateTime).Value = dateTime; } sql += " order by airport_identifier_code"; command.CommandText = sql; // execute query against airport_identifier // if the query fails in the preprocessor of sql server // an exception will be raised // links: // docLink: http://sql2x.org/documentationLink/9f9fcbf4-4764-4b2e-8dc6-41d0366c95c9 IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); // read all rows returned from the query of airport_identifier // read all columns from the datareader and // populate the List of C# objects with them // links: // docLink: http://sql2x.org/documentationLink/60181c7c-4a5e-41a3-9599-4e7a0aaf0cc8 while (reader.Read()) { var data = new CrudeAirportIdentifierData(); data.Populate(reader); dataList.Add(data); } } return(dataList); } }