Click here to Skip to main content
15,886,362 members
Articles / Database Development / SQL Server / SQL Server 2008

Simple Data Access in C#

Rate me:
Please Sign up or sign in to vote.
4.11/5 (15 votes)
11 Jan 2009CPOL5 min read 83.7K   1.4K   58  
Fast and easy to use data access class library.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using Yap.Data.Client;


namespace Yap.Data.UnitTest
{
	public class ClassicPersonRepository : IPersonRepository
	{
		public IList<Person> FindAll()
		{
			using (var connection = new SqlConnection(
				Scope.Configuration.ConnectionString))
			{
				using (var command = connection.CreateCommand())
				{
					command.CommandText = @"
SELECT
		Id,
		FirstName,
		LastName,
		Email,
		Login,
		Password,
		Question,
		Answer
	FROM
		Person";

					connection.Open();

					using (var reader = command.ExecuteReader())
					{
						var persons = new List<Person>();

						while (reader.Read())
						{
							persons.Add(
								new Person
								{
									Id = (Guid) reader["Id"],
									FirstName = (String) reader["FirstName"],
									LastName = (String) reader["LastName"],
									Email = (String) reader["Email"],
									Login = (String) reader["Email"],
									Password = (String) reader["Password"],
									Question = (String) reader["Question"],
									Answer = (String) reader["Answer"]
								});
						}

						return persons;
					}
				}
			}
		}

		public Person FindOne(Guid id)
		{
			using (var connection = new SqlConnection(
				Scope.Configuration.ConnectionString))
			{
				using (var command = connection.CreateCommand())
				{
					command.CommandText =
						@"
SELECT
		Id,
		FirstName,
		LastName,
		Email,
		Login,
		Password,
		Question,
		Answer
	FROM
		Person
	WHERE
		Id = @Id";
					command.Parameters.AddWithValue("Id", id);

					connection.Open();

					using (var reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							return new Person{
									Id = (Guid)reader["Id"],
									FirstName = (String)reader["FirstName"],
									LastName = (String)reader["LastName"],
									Email = (String)reader["Email"],
									Login = (String)reader["Login"],
									Password = (String)reader["Password"],
									Question = (String)reader["Question"],
									Answer = (String)reader["Answer"]
								};
						}

						return null;
					}
				}
			}
		}

		public void Insert(Person person)
		{
			using (var connection = new SqlConnection(
				Scope.Configuration.ConnectionString))
			{
				using (var command = connection.CreateCommand())
				{
					command.CommandText = @"
SET @Id = newid()
INSERT INTO
	Person
	(
		Id,
		FirstName,
		LastName,
		Email,
		Login,
		Password,
		Question,
		Answer
	)
	VALUES
	(
		@Id,
		@FirstName,
		@LastName,
		@Email,
		@Login,
		@Password,
		@Question,
		@Answer
	)";
					command.Parameters.Add(
						new SqlParameter
							{
								ParameterName = "Id",
								SqlDbType = SqlDbType.UniqueIdentifier,
								Direction = ParameterDirection.Output
							});
					command.Parameters.AddWithValue("FirstName", person.FirstName);
					command.Parameters.AddWithValue("LastName", person.LastName);
					command.Parameters.AddWithValue("Email", person.Email);
					command.Parameters.AddWithValue("Login", person.Login);
					command.Parameters.AddWithValue("Password", person.Password);
					command.Parameters.AddWithValue("Question", person.Question);
					command.Parameters.AddWithValue("Answer", person.Answer);

					connection.Open();

					command.ExecuteNonQuery();

					person.Id = (Guid)command.Parameters["Id"].Value;
				}
			}
		}

		public void Update(Person person)
		{
			using (var connection = new SqlConnection(
				Scope.Configuration.ConnectionString))
			{
				using (var command = connection.CreateCommand())
				{
					command.CommandText = @"
UPDATE
		Person
	SET
		FirstName = @FirstName,
		LastName = @LastName,
		Email = @Email,
		Login = @Login,
		Password = @Password,
		Question = @Question,
		Answer = @Answer
	WHERE
		Id = @Id";

					command.Parameters.AddWithValue("Id", person.Id);
					command.Parameters.AddWithValue("FirstName", person.FirstName);
					command.Parameters.AddWithValue("LastName", person.LastName);
					command.Parameters.AddWithValue("Email", person.Email);
					command.Parameters.AddWithValue("Login", person.Login);
					command.Parameters.AddWithValue("Password", person.Password);
					command.Parameters.AddWithValue("Question", person.Question);
					command.Parameters.AddWithValue("Answer", person.Answer);

					connection.Open();
					
					command.ExecuteNonQuery();
				}
			}
		}

		public void Delete(Person person)
		{
			using (var connection = new SqlConnection(
				Scope.Configuration.ConnectionString))
			{
				using (var command = connection.CreateCommand())
				{
					command.CommandText = @"
DELETE FROM
		Person
	WHERE
		Id = @Id";
					command.Parameters.AddWithValue("Id", person.Id);
					
					connection.Open();

					command.ExecuteNonQuery();
				}
			}
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions