Created
December 24, 2010 14:32
-
-
Save follesoe/754282 to your computer and use it in GitHub Desktop.
Re-discovering code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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