Preferred Language:
Listing 17.41 - App_Code\RandomDataLayer.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
public class RandomDataLayer
{
private static readonly string _connectionString;
public List<String> GetRandomMovies()
{
List<String> results = new List<String>();
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("GetRandomRows", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@rowsToReturn", 5);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
results.Add((string)reader["Title"]);
}
return results;
}
public static string GetRandomMovie()
{
string result = String.Empty;
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("GetRandomRow", con);
cmd.CommandType = CommandType.StoredProcedure;
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
result = (string)reader["Title"];
}
return result;
}
static RandomDataLayer()
{
_connectionString = WebConfigurationManager.ConnectionStrings["Movies"].ConnectionString;
}
}
ASP.NET 3.5 Unleashed
- Containing almost 2,000 pages of code samples and in-depth explanation of the
ASP.NET 3.5 Framework, ASP.NET 3.5 Unleashed is the most comprehensive book
written on the ASP.NET 3.5 Framework.
ASP.NET 3.5 Unleashed is now available in your local bookstore and online (Published January 7, 2008).
All of the code samples from this book are hosted "live" at this website.
Click here
to view the table of contents and code samples.