Exemple #1
0
        public void LookupBook( int index)
        {
            BookDetail book;

                using (var connection = new DatabaseConnectionProvider().GetConnection())
                {

                    SqlConnection sqlConnection = new SqlConnection("Data Source={0};Initial Catalog={1};User Id={2};Password={3}");

                    sqlConnection.Open();

                    var command = new SqlCommand
                    {
                        CommandType = CommandType.Text,
                        Connection = connection,
                        CommandText = "Select * from Books where bookid = @BookId"
                    };
                    command.Parameters.AddWithValue("@BookId", index);
                    using (var reader = command.ExecuteReader())
                    {

                        book = new BookDetail
                        {
                            Id = reader.GetInt32(reader.GetOrdinal("Id")),
                            Title = reader.GetString(reader.GetOrdinal("Title")),
                            Author = new AuthorDetail
                            {
                                FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
                                LastName = reader.GetString(reader.GetOrdinal("LastName"))
                            },
                        };
                    }

                }
        }
 /// <summary>
 /// Creates the entity.
 /// </summary>
 /// <param name="reader">The reader.</param>
 /// <returns></returns>
 private static IBookDetail CreateEntity(IDataReader reader)
 {
     var book = new BookDetail();
     if (reader == null)
     {
         return book;
     }
     while (reader.Read())
     {
         book = new BookDetail
                	{
                     Id = reader.GetInt32(reader.GetOrdinal("Id")),
                		Title = reader.GetString(reader.GetOrdinal("Title")),
                     Author = new AuthorDetail
                                  {
                                         FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
                                         LastName = reader.GetString(reader.GetOrdinal("LastName"))
                                   },
                	};
     }
     return book;
 }