/// <summary> /// Saves this ProductAsset object to the database. /// </summary> /// <returns><b>SaveResult</b> enumeration that represents the result of the save operation.</returns> public virtual SaveResult Save() { if (this.IsDirty) { Database database = Token.Instance.Database; bool recordExists = true; if (this.ProductAssetId == 0) { recordExists = false; } if (this.OrderBy < 0) { this.OrderBy = ProductAssetDataSource.GetNextOrderBy(this.ProductId); } if (recordExists) { //verify whether record is already present StringBuilder selectQuery = new StringBuilder(); selectQuery.Append("SELECT COUNT(*) As RecordCount FROM ac_ProductAssets"); selectQuery.Append(" WHERE ProductAssetId = @ProductAssetId"); using (DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString())) { database.AddInParameter(selectCommand, "@ProductAssetId", System.Data.DbType.Int32, this.ProductAssetId); if ((int)database.ExecuteScalar(selectCommand) == 0) { recordExists = false; } } } int result = 0; if (recordExists) { //UPDATE StringBuilder updateQuery = new StringBuilder(); updateQuery.Append("UPDATE ac_ProductAssets SET "); updateQuery.Append("ProductId = @ProductId"); updateQuery.Append(", AssetUrl = @AssetUrl"); updateQuery.Append(", OrderBy = @OrderBy"); updateQuery.Append(" WHERE ProductAssetId = @ProductAssetId"); using (DbCommand updateCommand = database.GetSqlStringCommand(updateQuery.ToString())) { database.AddInParameter(updateCommand, "@ProductAssetId", System.Data.DbType.Int32, this.ProductAssetId); database.AddInParameter(updateCommand, "@ProductId", System.Data.DbType.Int32, this.ProductId); database.AddInParameter(updateCommand, "@AssetUrl", System.Data.DbType.String, this.AssetUrl); database.AddInParameter(updateCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy); //RESULT IS NUMBER OF RECORDS AFFECTED result = database.ExecuteNonQuery(updateCommand); } } else { //INSERT StringBuilder insertQuery = new StringBuilder(); insertQuery.Append("INSERT INTO ac_ProductAssets (ProductId, AssetUrl, OrderBy)"); insertQuery.Append(" VALUES (@ProductId, @AssetUrl, @OrderBy)"); insertQuery.Append("; SELECT Scope_Identity()"); using (DbCommand insertCommand = database.GetSqlStringCommand(insertQuery.ToString())) { database.AddInParameter(insertCommand, "@ProductAssetId", System.Data.DbType.Int32, this.ProductAssetId); database.AddInParameter(insertCommand, "@ProductId", System.Data.DbType.Int32, this.ProductId); database.AddInParameter(insertCommand, "@AssetUrl", System.Data.DbType.String, this.AssetUrl); database.AddInParameter(insertCommand, "@OrderBy", System.Data.DbType.Int16, this.OrderBy); //RESULT IS NEW IDENTITY; result = AlwaysConvert.ToInt(database.ExecuteScalar(insertCommand)); this._ProductAssetId = result; } } //OBJECT IS DIRTY IF NO RECORDS WERE UPDATED OR INSERTED this.IsDirty = (result == 0); if (this.IsDirty) { return(SaveResult.Failed); } else { return(recordExists ? SaveResult.RecordUpdated : SaveResult.RecordInserted); } } //SAVE IS SUCCESSFUL IF OBJECT IS NOT DIRTY return(SaveResult.NotDirty); }
public static ProductAsset Load(Int32 productAssetId) { return(ProductAssetDataSource.Load(productAssetId, true)); }