בניית שכבת מידע עבור הטבלה RssFeed

סגור באמצעות טופס זה תוכלו לספר ולהמליץ לחבריכם..
שם השולח:
כתובת דוא"ל של השולח:
שם המקבל:
שלח לכתובת דוא"ל:
הוסף הערה:
כתיבת מחלקת Model ומחלקת Mapping עבור הטבלה RssFeed

בניית שכבת המידע עבור הטבלה RssFeed
מאת: ארז קלר
הטבלה RssFeed
הטבלה RssFeed שומרת את פרטי קבצי ה- RSS של האתר, בדרך כלל אתרים מפיצים מספר קבצי RSS המחולקים לתחומי עניין ולכן הקשר בין הטבלאות Website ו- RssFeed הוא יחיד-רבים (לכל אתר יש הרבה קבצי RSS, קובץ RSS  משויך לאתר אחד).
נבנה מחלקת Model ומחלקת Mapping עבור הטבלה הנוספת.
העיקרון הוא אותו העיקרון, התהליך הוא אותו התהליך.
מחלקת ה-Model:

 1  : public class RssFeed 
 2  : { 
 3  :     public int RssFeedID { get; set; } 
 4  :     public string Title { get; set; } 
 5  :     public string Link { get; set; } 
 6  :     public string Descriprion { get; set; } 
 7  :     public int WebsiteID { get; set; } 
 8  :     public int CategoryID { get; set; } 
 9  : } 

מחלקת האוסף (Model List):
 1  : public class RssFeeds : List<RssFeed> 
 2  : { 
 3  :     public List<RssFeed> OrderByTitle() 
 4  :     { 
 5  :         if (Count > 0) 
 6  :         { 
 7  :             return this.OrderBy(item => item.Title).ToList(); 
 8  :         } 
 9  :         return null; 
 10 :     } 
 11 :     public List<RssFeed> SelectByTitle(string title) 
 12 :     { 
 13 :         if (Count > 0) 
 14 :         { 
 15 :             return this.Where(item => 
 16 :                       item.Title.StartsWith(title)).ToList(); 
 17 :         } 
 18 :         return null; 
 19 :     } 
 20 : } 

מחלקת ה-Mapping:
 1  : public class RssFeedDB 
 2  : { 
 3  :     private string connectionString = @"Data Source=CORNER\SQLEXPRESS;Initial Catalog=RssDB;Integrated Security=True"; 
 4  :     private SqlConnection connection; 
 5  :     private SqlCommand command; 
 6  :     private SqlDataReader reader; 
 7  :     public RssFeedDB() 
 8  :     { 
 9  :         connection = new SqlConnection(connectionString); 
 10 :         command = new SqlCommand(); 
 11 :         command.Connection = connection; 
 12 :     } 
 13 :  
 14 :     public RssFeeds SelectAll() 
 15 :     { 
 16 :         command.CommandText = "SELECT * FROM RssFeed"; 
 17 :         RssFeeds RssFeeds = Select(); 
 18 :         return RssFeeds; 
 19 :     } 
 20 :     public RssFeeds SelectByTitle(string title) 
 21 :     { 
 22 :         command.CommandText = string.Format("SELECT * FROM RssFeed WHERE Title = {0}", title); 
 23 :         RssFeeds RssFeeds = Select(); 
 24 :         return RssFeeds; 
 25 :     } 
 26 :     public RssFeed SelectByID(string id) 
 27 :     { 
 28 :         command.CommandText = string.Format("SELECT * FROM RssFeed WHERE RssFeedID = {0}", id); 
 29 :         RssFeeds RssFeeds = Select(); 
 30 :         if (RssFeeds.Count() == 1) 
 31 :             return RssFeeds[0]; 
 32 :         return null; 
 33 :     } 
 34 :     private RssFeeds Select() 
 35 :     { 
 36 :         RssFeeds RssFeeds = null; 
 37 :         try 
 38 :         { 
 39 :             command.Connection = connection; 
 40 :             connection.Open(); 
 41 :             reader = command.ExecuteReader(); 
 42 :             RssFeeds = CreateModel(); 
 43 :         } 
 44 :         catch (Exception e) 
 45 :         { 
 46 :  
 47 :         } 
 48 :         finally 
 49 :         { 
 50 :             if (reader != null) 
 51 :                 reader.Close(); 
 52 :             if (connection.State == ConnectionState.Open) 
 53 :                 connection.Close(); 
 54 :         } 
 55 :         return RssFeeds; 
 56 :     } 
 57 :     private RssFeeds CreateModel() 
 58 :     { 
 59 :         RssFeeds RssFeeds = new RssFeeds(); 
 60 :         RssFeed RssFeed; 
 61 :         while (reader.Read()) 
 62 :         { 
 63 :             RssFeed = new RssFeed(); 
 64 :             RssFeed.RssFeedID = (int)reader["RssFeedID"]; 
 65 :             RssFeed.Title = reader["Title"].ToString(); 
 66 :             RssFeed.Link = reader["Link"].ToString(); 
 67 :             RssFeed.Descriprion = reader["Descriprion"].ToString(); 
 68 :             RssFeed.RssFeedID = (int)reader["RssFeedID"]; 
 69 :             RssFeed.CategoryID = (int)reader["CategoryID"]; 
 70 :             RssFeeds.Add(RssFeed); 
 71 :         } 
 72 :         return RssFeeds; 
 73 :     } 
 74 :     public int Insert(RssFeed rss_feed) 
 75 :     { 
 76 :         StringBuilder sql_builder = new StringBuilder(); 
 77 :         sql_builder.AppendFormat("INSERT INTO RssFeed (Title, Link, Description, WebsiteID, CategoryID) VALUES ('{0}','{1}', '{2}','{3}','{4}', ) ", 
 78 :             rss_feed.Title, rss_feed.Link, rss_feed.Descriprion, rss_feed.WebsiteID, rss_feed.CategoryID); 
 79 :         return SaveChanges(sql_builder.ToString()); 
 80 :     } 
 81 :     public int Update(RssFeed rss_feed) 
 82 :     { 
 83 :         StringBuilder sql_builder = new StringBuilder(); 
 84 :         sql_builder.AppendFormat("UPDATE RssFeed SET Title='{0}', Link='{1}', Description ='{2}', WebsiteID='{3}', CategoryID='{4}' WHERE RssFeedID = {5}", 
 85 :             rss_feed.Title, rss_feed.Link, rss_feed.Link, rss_feed.Descriprion, rss_feed.WebsiteID, rss_feed.CategoryID, rss_feed.RssFeedID); 
 86 :         return SaveChanges(sql_builder.ToString()); 
 87 :     } 
 88 :     public int Delete(RssFeed rss_feed) 
 89 :     { 
 90 :         StringBuilder sql_builder = new StringBuilder(); 
 91 :         sql_builder.AppendFormat("DELETE FROM RssFeed WHERE RssFeedID = {0}", rss_feed.RssFeedID); 
 92 :         return SaveChanges(sql_builder.ToString()); 
 93 :     } 
 94 :     private int SaveChanges(string command_text) 
 95 :     { 
 96 :         int records = 0; 
 97 :         try 
 98 :         { 
 99 :             command.CommandText = command_text; 
 100:             connection.Open(); 
 101:             records = command.ExecuteNonQuery(); 
 102:         } 
 103:         catch (Exception e) 
 104:         { 
 105:  
 106:         } 
 107:         finally 
 108:         { 
 109:             connection.Close(); 
 110:         } 
 111:         return records; 
 112:     } 
 113: } 
 

 



 
 



כל הזכויות שמורות למחבר ©