public virtual IList Find(Blog blog) { // As you see, no transaction involved here IList list = new ArrayList(); using(IDbConnection conn = _connFactory.CreateConnection()) { IDbCommand command = conn.CreateCommand(); // Not the best way, but the simplest command.CommandText = String.Format("select id, title, contents from posts where blogid = {0} " + "order by id", blog.Id); using (IDataReader reader = command.ExecuteReader()) { while(reader.Read()) { Post post = new Post( reader.GetInt32(0), blog, reader.GetString(1), reader.GetString(2) ); list.Add(post); } } } return list; }
public virtual Blog Create(Blog blog) { using(IDbConnection conn = _connFactory.CreateConnection()) { IDbCommand command = conn.CreateCommand(); // Not the best way, but the simplest command.CommandText = String.Format("INSERT INTO blogs (name, author) values ('{0}', '{1}');select @@identity", blog.Name, blog.Author); object result = command.ExecuteScalar(); blog.Id = Convert.ToInt32(result); } return blog; }
public Post(int id, Blog blog, string title, string contents) : this(blog, title, contents) { _id = id; }
public Post(Blog blog, string title, string contents) { _blog = blog; _title = title; _contents = contents; }
public virtual IList Find() { // As you see, no transaction involved here IList list = new ArrayList(); using(IDbConnection conn = _connFactory.CreateConnection()) { IDbCommand command = conn.CreateCommand(); // Not the best way, but the simplest command.CommandText = "Select id, name, author from blogs"; using (IDataReader reader = command.ExecuteReader()) { while(reader.Read()) { Blog blog = new Blog( reader.GetInt32(0), reader.GetString(1), reader.GetString(2) ); list.Add(blog); } } } return list; }