// fetch all from table into new List of class instances, only populating specific columns, // with a limit on number of returned rows and order by columns starting at a specific row // links: // docLink: http://sql2x.org/documentationLink/12d2812e-e963-4f26-8014-48c4e9cfb3ae public static List <CrudeProductData> FetchAllWithLimitAndOffset(int limit, int offset) { var dataList = new List <CrudeProductData>(); // create query against product // 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/21eca289-4747-4a75-b0a2-1a58457be608 string sql = @" select product_id, product_became_id, product_name, state_rcd, user_id, date_time from [product] order by product_name"; // 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/c3e624a4-8479-4c17-bec7-ec09f3abbe64 using (var conn = new SqlConnection(ConfigurationManager.AppSettings["Conn"])) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // execute query against product // if the query fails in the preprocessor of sql server // an exception will be raised // links: // docLink: http://sql2x.org/documentationLink/6507b543-adbc-4863-810b-8db439f40d5c IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); int count = 0; // read all rows returned from the query of product // read all columns from the datareader and // populate the List of C# objects with them // links: // docLink: http://sql2x.org/documentationLink/fcd15e2f-df3f-44d8-8a13-d2e93d97f685 while (reader.Read()) { if ((count >= offset) && (count <= offset + limit)) { var data = new CrudeProductData(); data.Populate(reader); dataList.Add(data); } count++; if (count > limit + offset) { break; } } } return(dataList); } }
// fetch by Foreign key into new List of class instances // links: // docLink: http://sql2x.org/documentationLink/13095cd7-f136-4532-8969-c50c62cc05ef public static List <CrudeProductData> FetchByStateRcd(string stateRcd) { var dataList = new List <CrudeProductData>(); // create query against product // 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/86c78f05-a65f-4dfe-b048-d0cbece49f4e string sql = @" select product_id, product_became_id, product_name, state_rcd, user_id, date_time from [product] where state_rcd = @state_rcd order by product_name"; // 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/6fd25822-459f-4796-803a-071a3a270aa0 using (var conn = new SqlConnection(ConfigurationManager.AppSettings["Conn"])) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // add foreign key column // this foreign key column will be used together with the prepared ansi sql statement // links: // docLink: http://sql2x.org/documentationLink/8aec2e5c-4732-4662-badb-83b89d1c34a9 command.Parameters.Add("@state_rcd", SqlDbType.NVarChar).Value = stateRcd.Replace("'", "''"); // execute query against product // if the query fails in the preprocessor of sql server // an exception will be raised // links: // docLink: http://sql2x.org/documentationLink/bb9abe5c-d2c1-455a-b597-a2af0a35de5a IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); // read all rows returned from the query of product // read all columns from the datareader and // populate the List of C# objects with them // links: // docLink: http://sql2x.org/documentationLink/5c413c03-5ddc-472a-a63d-53aecc7a2573 while (reader.Read()) { var data = new CrudeProductData(); data.Populate(reader); dataList.Add(data); } } return(dataList); } }
// fetch by Primary key into new class instance // links: // docLink: http://sql2x.org/documentationLink/7a625d0a-3028-42ce-a543-72ea3673cef4 // parameters: // productId: primary key of table product public static CrudeProductData GetByProductId(System.Guid productId) { // create query against product // 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/72f5fc83-c460-40fe-bac9-73b7ee0c6b77 string sql = @" select top 1 product_id, product_became_id, product_name, state_rcd, user_id, date_time from [product] where product_id = @product_id order by product_name"; var ret = new CrudeProductData(); // 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/1ecca728-0c07-4dd7-b095-d10777b25b70 using (var conn = new SqlConnection(ConfigurationManager.AppSettings["Conn"])) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // add primary key // this primary key will be used together with the prepared ansi sql statement // links: // docLink: http://sql2x.org/documentationLink/1ecca728-0c07-4dd7-b095-d10777b25b70 command.Parameters.Add("@product_id", SqlDbType.UniqueIdentifier).Value = productId; // execute query against product // if the query fails in the preprocessor of sql server // an exception will be raised // links: // docLink: http://sql2x.org/documentationLink/fd5c5faa-b400-4f29-b12b-9675c53a757f IDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow); // populate serialized class if a row was found if (reader.Read()) { ret.Populate(reader); } } } return(ret); }
// fetch all from table into new List of class instances, with a limit on number of returned rows and order by columns // links: // docLink: http://sql2x.org/documentationLink/dfaa482b-059b-4f17-a9a9-4885138dbb46 public static List <CrudeProductData> FetchAllWithLimit(int limit) { var dataList = new List <CrudeProductData>(); // create query against product // 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/41f82773-6d37-4ebe-840c-c60e06337f45 string sql = @" select top " + limit.ToString() + @" product_id, product_became_id, product_name, state_rcd, user_id, date_time from [product] order by product_name"; // 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/da228d98-b30e-4d79-89ae-98e813437753 using (var conn = new SqlConnection(ConfigurationManager.AppSettings["Conn"])) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // execute query against product // if the query fails in the preprocessor of sql server // an exception will be raised // links: // docLink: http://sql2x.org/documentationLink/c32ad724-8a03-4b4c-b6fb-a5abfb1d707e IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); // read all rows returned from the query of product // read all columns from the datareader and // populate the List of C# objects with them // links: // docLink: http://sql2x.org/documentationLink/9ba4395d-d8a4-4427-b80f-7b828e34da7a while (reader.Read()) { var data = new CrudeProductData(); data.Populate(reader); dataList.Add(data); } } return(dataList); } }
// fetch all rows from table product into new List of class instances // links: // docLink: http://sql2x.org/documentationLink/7ca0c014-527e-4a0a-bd1f-12f4d8ea4b43 public static List <CrudeProductData> FetchAll() { var dataList = new List <CrudeProductData>(); // create query against product // 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 product_id, product_became_id, product_name, state_rcd, user_id, date_time from [product] order by product_name"; // 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(ConfigurationManager.AppSettings["Conn"])) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { // execute query against product // 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 product // 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 CrudeProductData(); data.Populate(reader); dataList.Add(data); } } return(dataList); } }
public Product() { // product base info product = new CrudeProductData(); // product attributes ProductAttribute = new List <CrudeProductAttributeData>(); // product identifiers ProductIdentifier = new List <CrudeProductIdentifierData>(); // product information links ProductInfo = new List <CrudeProductInfoData>(); // product images ProductImage = new List <CrudeProductImageData>(); // product documentation ProductDocumentation = new List <CrudeProductDocumentationData>(); }
public Product(Guid productId) { // product base info product = new CrudeProductData(); product.FetchByProductId(productId); // product attributes ProductAttribute = CrudeProductAttributeData.FetchByProductId(productId); // product identifiers ProductIdentifier = CrudeProductIdentifierData.FetchByProductId(productId); // product information links ProductInfo = CrudeProductInfoData.FetchByProductId(productId); // product images ProductImage = CrudeProductImageData.FetchByProductId(productId); // product documentation ProductDocumentation = CrudeProductDocumentationData.FetchByProductId(productId); }
public void Save(Guid userId) { // start transaction using (var connection = new SqlConnection(ConfigurationManager.AppSettings["Conn"])) { connection.Open(); SqlTransaction transaction = connection.BeginTransaction(); try { // keep copy of 'old' product CrudeProductData oldProduct = ObjectCopier.Clone(product); // save new product product.ProductId = Guid.NewGuid(); product.StateRcd = DefaultStateRef.Updated; product.UserId = userId; product.DateTime = DateTime.UtcNow; product.Insert(connection, transaction); // duplicate old products category mapping CrudeProductCategoryData.InsertNewCategoryMapping( connection, transaction, oldProduct.ProductId, product.ProductId, userId ); // update product to be 'old' version oldProduct.ProductBecameId = product.ProductId; oldProduct.UpdateBecame(connection, transaction); // change identifier keys, save foreach (CrudeProductIdentifierData identifier in ProductIdentifier) { identifier.ProductIdentifierId = Guid.NewGuid(); identifier.ProductId = product.ProductId; identifier.UserId = userId; identifier.DateTime = DateTime.UtcNow; identifier.Insert(connection, transaction); } // change attribute keys, save foreach (CrudeProductAttributeData attribute in ProductAttribute) { attribute.ProductAttributeId = Guid.NewGuid(); attribute.ProductId = product.ProductId; attribute.UserId = userId; attribute.DateTime = DateTime.UtcNow; attribute.Insert(connection, transaction); } // change info keys, save foreach (CrudeProductInfoData info in ProductInfo) { info.ProductInfoId = Guid.NewGuid(); info.ProductId = product.ProductId; info.UserId = userId; info.DateTime = DateTime.UtcNow; info.Insert(connection, transaction); } // change documentation keys, save foreach (CrudeProductDocumentationData documentation in ProductDocumentation) { documentation.ProductDocumentationId = Guid.NewGuid(); documentation.ProductId = product.ProductId; documentation.UserId = userId; documentation.DateTime = DateTime.UtcNow; documentation.Insert(connection, transaction); } // change image keys, save foreach (CrudeProductImageData image in ProductImage) { image.ProductImageId = Guid.NewGuid(); image.ProductId = product.ProductId; image.UserId = userId; image.DateTime = DateTime.UtcNow; image.Insert(connection, transaction); } // commit transaction transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception("DataAccessLayer, Product, Save, Failed", ex); } } }
// 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 <CrudeProductData> FetchWithFilter(System.Guid productId, System.Guid productBecameId, string productName, string stateRcd, System.Guid userId, System.DateTime dateTime) { var dataList = new List <CrudeProductData>(); // create query against product // 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 product_id, product_became_id, product_name, state_rcd, user_id, date_time from [product] 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(ConfigurationManager.AppSettings["Conn"])) { 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 (productId != Guid.Empty) { sql += " and product_id = @product_id"; command.Parameters.Add("@product_id", SqlDbType.UniqueIdentifier).Value = productId; } if (productBecameId != Guid.Empty) { sql += " and product_became_id = @product_became_id"; command.Parameters.Add("@product_became_id", SqlDbType.UniqueIdentifier).Value = productBecameId; } if (!string.IsNullOrEmpty(productName)) { sql += " and product_name like '%' + @product_name + '%'"; command.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = productName.Replace("'", "''"); } if (!string.IsNullOrEmpty(stateRcd)) { sql += " and state_rcd like '%' + @state_rcd + '%'"; command.Parameters.Add("@state_rcd", SqlDbType.NVarChar).Value = stateRcd.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 product_name"; command.CommandText = sql; // execute query against product // 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 product // 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 CrudeProductData(); data.Populate(reader); dataList.Add(data); } } return(dataList); } }