Exemplo n.º 1
0
        public Grant Save(Grant grant)
        {
            if (grant == null)
            {
                return(null);
            }

            // Assumptions: If an identifier is not provided, it's an insert.
            // Otherwise, it's an update.
            if (string.IsNullOrEmpty(grant.GrantId))
            {
                grant.GrantId = NextId();
                if (!Insert(grant))
                {
                    // What do we do on failure?...
                }
            }
            else
            {
                if (!Update(grant))
                {
                    // What do we do on failure?...
                }
            }

            return(grant);
        }
Exemplo n.º 2
0
        private bool Insert(Grant grant)
        {
            using (var connection = Connector.GetOpenConnection(CONN_STRING_KEY))
            {
                var command = connection.CreateCommand();
                command.CommandText = "INSERT INTO [Grant] (GrantId, GrantName, EmpID, Amount) VALUES " +
                                      "(@GrantId, @GrantName, @EmpId, @Amount); ";

                command.AddParm("@GrantID", grant.GrantId, DbType.AnsiStringFixedLength, 3);
                command.AddParm("@GrantName", grant.GrantName, DbType.String);
                command.AddParm("@EmpID", grant.EmpID.HasValue ? (object)grant.EmpID.Value : DBNull.Value, DbType.Int32);
                command.AddParm("@Amount", grant.Amount, DbType.Decimal);

                return(command.ExecuteNonQuery() > 0);
            }
        }
Exemplo n.º 3
0
        private bool Update(Grant grant)
        {
            using (var connection = Connector.GetOpenConnection(CONN_STRING_KEY))
            {
                var command = connection.CreateCommand();
                command.CommandText = "UPDATE [Grant] SET " +
                                      "GrantName = @GrantName, " +
                                      "EmpID = @EmpID, " +
                                      "Amount = @Amount " +
                                      "WHERE GrantID = @GrantID;";

                command.AddParm("@GrantID", grant.GrantId, DbType.AnsiStringFixedLength, 3);
                command.AddParm("@GrantName", grant.GrantName, DbType.String);
                command.AddParm("@EmpID", grant.EmpID.HasValue ? (object)grant.EmpID.Value : DBNull.Value, DbType.Int32);
                command.AddParm("@Amount", grant.Amount, DbType.Decimal);

                return(command.ExecuteNonQuery() > 0);
            }
        }