static void TestAddCatalogItem() { PrintTitle("Testing AddCatalogItem"); string[,] data = new string[3, 4] { {"VOACHINESE", "RSS", "News feed from VOA Chinese", "http://www.voanews.com/chinese/rss"}, {"RFACHINESE", "RSS", "News feed from RFA Chinese", "http://www.rfa.com/chinese/rss"}, {"VOAENGLISH", "RSS", "News feed from VOA English", "http://www.voanews.com/rss"} }; Console.WriteLine("Adding 3 catalog items..."); for (int i = 0; i < 3; i++) { CatalogItem item = new CatalogItem(); item.Code = data[i, 0]; item.ContentType = data[i, 1]; item.Description = data[i, 2]; item.Location = data[i, 3]; FoeServerCatalog.AddCatalogItem(item); } }
public static void AddCatalogItem(CatalogItem item) { // get catalog items from DB SqlConnection conn = FoeServerDb.OpenDb(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into CatalogRss (Code, ContentType, Description, Location) " + "values (@code, @contentType, @description, @location)"; cmd.Parameters.Add("@code", SqlDbType.NVarChar, 10); cmd.Parameters.Add("@contentType", SqlDbType.NVarChar, 32); cmd.Parameters.Add("@description", SqlDbType.NVarChar, 512); cmd.Parameters.Add("@location", SqlDbType.NVarChar, 512); cmd.Prepare(); // populate data cmd.Parameters["@code"].Value = item.Code; cmd.Parameters["@contentType"].Value = item.ContentType; cmd.Parameters["@description"].Value = item.Description; cmd.Parameters["@location"].Value = item.Location; // get item cmd.ExecuteNonQuery(); conn.Close(); }
/// <summary> /// Get all catalog items available. /// </summary> /// <returns>A list of catalog items that are available.</returns> public static List<CatalogItem> GetCatalog() { List<CatalogItem> items = new List<CatalogItem>(); // get catalog items from DB SqlConnection conn = FoeServerDb.OpenDb(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from CatalogRss order by Name"; // get items SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { // create a new item and populate the data CatalogItem item = new CatalogItem(); item.Code = FoeServerDb.GetString(reader, "Code"); item.ContentType = FoeServerDb.GetString(reader, "ContentType"); item.Description = FoeServerDb.GetString(reader, "Description"); item.Location = FoeServerDb.GetString(reader, "Location"); // add to list items.Add(item); } reader.Close(); conn.Close(); return items; }
public static void UpdateCatalogItem(CatalogItem item) { // get catalog items from DB SqlConnection conn = FoeServerDb.OpenDb(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "update CatalogRss set " + "ContentType=@contentType, " + "Description=@description, " + "Location=@location " + "where Code=@code"; cmd.Parameters.Add("@code", SqlDbType.NVarChar, 10); cmd.Parameters.Add("@contentType", SqlDbType.NVarChar, 32); cmd.Parameters.Add("@description", SqlDbType.NVarChar, 512); cmd.Parameters.Add("@location", SqlDbType.NVarChar, 512); cmd.Prepare(); // populate data cmd.Parameters["@code"].Value = item.Code; cmd.Parameters["@contentType"].Value = item.ContentType; cmd.Parameters["@description"].Value = item.Description; cmd.Parameters["@location"].Value = item.Location; // get item cmd.ExecuteNonQuery(); conn.Close(); }
/// <summary> /// Get catalog item that matches the catalog code. /// </summary> /// <param name="code">Catalog code</param> /// <returns>CatalogItem object that contains the details of a catalog item. If no matching catalog code is found, null will be returned.</returns> public static CatalogItem GetCatalogItem(string code) { CatalogItem item = null; // get catalog items from DB SqlConnection conn = FoeServerDb.OpenDb(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from CatalogRss where Code=@code"; cmd.Parameters.Add("@code", SqlDbType.NVarChar, 10); cmd.Prepare(); cmd.Parameters["@code"].Value = code; // get item SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { // populate the data item = new CatalogItem(); item.Code = FoeServerDb.GetString(reader, "Code"); item.ContentType = FoeServerDb.GetString(reader, "ContentType"); item.Description = FoeServerDb.GetString(reader, "Description"); item.Location = FoeServerDb.GetString(reader, "Location"); } reader.Close(); conn.Close(); return item; }