public virtual Post Update(Post post) { using(IDbConnection conn = _connFactory.CreateConnection()) { IDbCommand command = conn.CreateCommand(); command.CommandText = String.Format("UPDATE posts set title = '{0}', contents = '{1}' where id = {2}", post.Title, post.Contents, post.Id); command.ExecuteNonQuery(); } return post; }
public virtual Post Create(Post post) { using(IDbConnection conn = _connFactory.CreateConnection()) { IDbCommand command = conn.CreateCommand(); // Not the best way, but the simplest command.CommandText = String.Format("INSERT INTO posts (title, contents, blogid) values ('{0}', '{1}', {2});select @@identity", post.Title, post.Contents, post.Blog.Id); object result = command.ExecuteScalar(); post.Id = Convert.ToInt32(result); } return post; }
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; }