コード例 #1
0
ファイル: ContactDao.cs プロジェクト: stojaget/Contacts
        /// <summary>
        /// Initializes a new instance of the Contact class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static Contact FillDataRecord(IDataRecord myDataRecord)
        {
            Contact myContact = new Contact();

            myContact.Id = myDataRecord.GetInt32(myDataRecord.GetOrdinal("Id"));
            myContact.FirstName = myDataRecord.GetString(myDataRecord.GetOrdinal("FirstName"));

            myContact.LastName = myDataRecord.GetString(myDataRecord.GetOrdinal("LastName"));
            myContact.Phone = myDataRecord.GetString(myDataRecord.GetOrdinal("Phone"));
            myContact.Email = myDataRecord.GetString(myDataRecord.GetOrdinal("Email"));
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DateCreated")))
            {
                myContact.DateCreated = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("DateCreated"));
            }

            return myContact;
        }
コード例 #2
0
ファイル: ContactDao.cs プロジェクト: stojaget/Contacts
        /// <summary>Saves a contact person in the database.</summary>
        /// <param name="myContact">The Contact instance to save.</param>
        /// <returns>The new ID if the Contact is new in the database or the existing ID when an item was updated.</returns>
        public static int Save(Contact myContact)
        {
            if (!myContact.Validate())
            {
                throw new InvalidSaveOperationException("Can't save a contact in an Invalid state.");
            }
            try
            {
                int result = 0;
                using (SqlConnection myConnection = new SqlConnection(AppConfig.ConnectionString))
                {
                    using (SqlCommand myCommand = new SqlCommand("sprocContactInsertUpdate", myConnection))
                    {
                        myCommand.CommandType = CommandType.StoredProcedure;

                        myCommand.Parameters.AddWithValue("@firstName", myContact.FirstName);
                        myCommand.Parameters.AddWithValue("@lastName", myContact.LastName);
                        myCommand.Parameters.AddWithValue("@phone", myContact.Phone);
                        myCommand.Parameters.AddWithValue("@email", myContact.Email);
                        myCommand.Parameters.AddWithValue("@dateCreated", myContact.DateCreated);

                        Helpers.SetSaveParameters(myCommand, myContact);

                        myConnection.Open();
                        int numberOfRecordsAffected = myCommand.ExecuteNonQuery();
                        if (numberOfRecordsAffected == 0)
                        {
                            throw new DBConcurrencyException("Can't update contact as it has been updated by someone else");
                        }

                        result = Helpers.GetBusinessBaseId(myCommand);
                    }
                    myConnection.Close();
                }
                return result;
            }
            catch (Exception ex)
            {

                throw new DataAccessException(Exceptions.EX01, ex);
            }
        }