Click here to Skip to main content
15,897,519 members
Articles / Web Development / ASP.NET

Photo album browsing control

Rate me:
Please Sign up or sign in to vote.
4.64/5 (30 votes)
1 Dec 20044 min read 345.2K   6.8K   138  
A web control that allows photo album browsing
using System;
using System.Data;
using System.Data.OleDb;
using Codefresh.PhotoBrowserLibrary;
using Codefresh.PhotoBrowserLibrary.Collections;

namespace Codefresh.PhotoBrowserLibrary.DataAccessLayer
{
	/// <summary>
	/// Database access class for Comment objects.
	/// </summary>
	internal class CommentDB : DBBase
	{

		/// <summary>
		/// Intialises a new CommentDB object.
		/// </summary>
		/// <param name="conn">A reference to a currently open database connection.</param>
		public CommentDB(IDbConnection conn) : base(conn)
		{
		}

		/// <summary>
		/// Inserts a new comment into the databse
		/// </summary>
		/// <param name="photo">A reference to the photo that the comment belongs to.</param>
		/// <param name="comment">The comment to insert.</param>
		/// <returns>The ID of the inserted comment.</returns>
		public int Insert(Photo photo, Comment comment)
		{

			IDbCommand cmd = GetCommand();
			cmd.CommandText = "INSERT INTO tblPhotoComments (PhotoFullVirtualPath, Name, Comment, DateAdded) VALUES (?, ?, ?, ?)";

			cmd.Parameters.Add(CreateStringParam("PhotoFullVirtualPath", photo.FullVirtualPath)); 
			cmd.Parameters.Add(CreateStringParam("Name", comment.Name)); 
			cmd.Parameters.Add(CreateStringParam("Comment", comment.CommentText));
			cmd.Parameters.Add(CreateDateParam("DateAdded", comment.DateAdded)); 

			cmd.ExecuteNonQuery();

			return GetIdentityValue(comment);

		}

		/// <summary>
		/// Retrieves the list of comments for a photo.
		/// </summary>
		/// <param name="token">A reference to the current SessionToken object.</param>
		/// <param name="photo">The photo to retrieve the list of comments for.</param>
		/// <returns>A collection of Comment objects.</returns>
		public Comments GetComments(SessionToken token, Photo photo)
		{

			IDbCommand cmd = GetCommand();
			cmd.CommandText = "SELECT ID, Name, Comment, DateAdded FROM tblPhotoComments WHERE PhotoFullVirtualPath = ? " +
							  "ORDER BY DateAdded, ID";

			cmd.Parameters.Add(CreateStringParam("PhotoFullVirtualPath", photo.FullVirtualPath)); 

			Comments results = new Comments();

			using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.Default))
			{

				while(reader.Read()) 
				{

					Comment comment = new Comment(token,
												  reader.GetInt32(0), 
												  reader.IsDBNull(1) ? "" : reader.GetString(1), 
 												  reader.IsDBNull(2) ? "" : reader.GetString(2), 
												  reader.GetDateTime(3));

					results.Add(comment);
	
				}

			}

			return results;

		}

		/// <summary>
		/// Not used as you cannot currently delete comments.
		/// </summary>
		/// <param name="obj">A reference to the comment to delete.</param>
		internal override void Delete(PhotoObjectBase obj)
		{
			throw new NotImplementedException("Cannot delete comment objects");
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
I'm currently working in London working on a variety of technologies.

Comments and Discussions