Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created December 24, 2010 14:32
Show Gist options
  • Save follesoe/754282 to your computer and use it in GitHub Desktop.
Save follesoe/754282 to your computer and use it in GitHub Desktop.
Re-discovering code snippets
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
{
SqlCommand command = new SqlCommand("GetItems", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@author", "Jonas Folleso");
using (command)
{
command.Connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
List<Item> items = new List<Item>();
while (reader.Read())
{
Item item = new Item();
item.ID = reader["id"] == null ? -1 : Convert.ToInt32(reader["id"]);
item.Title = reader["title"] == null ? string.Empty : reader["title"] as string;
items.Add(item);
}
reader.Close();
}
command.Connection.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment