|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace DomainObjects
{
public class SQLDataAccess
{
private static string connectionString = "Server=localhost;Database=School;Trusted_Connection=true";
public static void UpdatePerson(Person person)
{
string query = @"UPDATE Persons SET FirstName = @FirstName, LastName = @LastName, DOB = @DOB, DateModified = GETDATE()
WHERE PersonID = @PersonID";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.Parameters.AddWithValue("@FirstName", person.FirstName);
myCommand.Parameters.AddWithValue("@LastName", person.LastName);
myCommand.Parameters.AddWithValue("@DOB", person.DOB);
myCommand.Parameters.AddWithValue("@PersonID", person.Id);
conn.Open();
myCommand.ExecuteNonQuery();
}
}
public static void SaveAddress(IList<Address> addresses)
{
string query = "INSERT INTO Addresses(PersonID,Street) VALUES(@PersonID, @Street) SELECT SCOPE_IDENTITY()";
foreach (Address address in addresses)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.Parameters.AddWithValue("@PersonID", address.Person.Id);
myCommand.Parameters.AddWithValue("@Street", address.Street);
conn.Open();
address.Id = Convert.ToInt32(myCommand.ExecuteScalar());
myCommand.Parameters.Clear();
}
}
}
public static void SavePerson(Person person)
{
string query = "INSERT INTO Persons(FirstName,LastName,DOB) VALUES(@FirstName,@LastName,@DOB) SELECT SCOPE_IDENTITY()";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.Parameters.AddWithValue("@FirstName", person.FirstName);
myCommand.Parameters.AddWithValue("@LastName", person.LastName);
myCommand.Parameters.AddWithValue("@DOB", person.DOB);
conn.Open();
person.Id = Convert.ToInt32(myCommand.ExecuteScalar());
// save the addresses!
SaveAddress(person.Addresses);
}
}
public static Person GetPersonById(int id)
{
/*
* we used left outer join since we want to return the person object even if it does not have
* the corresponding address object!
* */
string query = @"SELECT p.PersonID,p.FirstName,p.LastName,p.DOB,a.street FROM Persons p
left outer join addresses a on a.personid = p.personid
WHERE p.PersonID = @PersonID";
Person person = new Person();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.Parameters.AddWithValue("@PersonID", id);
conn.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
person.Id = (int)reader["PersonID"];
person.FirstName = reader["FirstName"] as String;
person.LastName = reader["LastName"] as String;
person.DOB = Convert.ToDateTime(reader["DOB"]);
// add the address to the person object!
person.AddAddress(new Address(person) { Street = reader["Street"] as String });
};
}
return person;
}
}
}
|
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.
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.
I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses