בניית שכבת נתונים עבור טבלה בודדת - שלב ראשון

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

בניית שכבת נתונים עבור טבלה בודדת  
שלב ראשון

מאת: ארז קלר

במסד הנתונים מוגדרת הטבלה WebSite המכילה את הפרטים הנדרשים על כל אחד מהאתרים המפיצים את קבצי ה-RSS.
כל רשומה מכילה פרטים אודות אתר המפיץ את קבצי ה- RSS.
website table diagram

מחלקת המודל
תחילה נגדיר את מחלקת המודל WebSite המהווה "מראה" לטבלה Website הקיימת במסד הנתונים:

 1  : public class Website 
 2  : { 
 3  :     public int WebsiteID { get; set; } 
 4  :     public string Name { get; set; } 
 5  :     public string LogoLink { get; set; } 
 6  :     public string Link { get; set; } 
 7  : } 
 
בתוכנית נטפל במספר אתרים המפיצים מידע בקבצי RSS, נוסיף מחלקה נוספת המטפלת באוסף של האתרים הללו:

 1  : public class Websites : List<Website> 
 2  : { 
 3  :     
 4  : }
 
ברצותנו, ניתן להוסיף למחלקת האוסף מתודות שונות על מנת להוסיף יכולות עיבוד שונות על אוסף האובייקטים, לדוגמה: מיון על פי שדה זה או אחר, חיפוש אובייקט מסוים על פי פרמטר זה או אחר וכדומה.
לדוגמה:
 1  : public class Websites : List<Website> 
 2  : { 
 3  :     public List<Website> OrderByName() 
 4  :     { 
 5  :         if (Count > 0) 
 6  :         { 
 7  :             return this.OrderBy(item => item.Name).ToList(); 
 8  :         } 
 9  :         return null; 
 10 :     } 
 11 :     public List<Website> SelectByName(string name) 
 12 :     { 
 13 :         if (Count > 0) 
 14 :         { 
 15 :             return this.Where(item => item.Name.StartsWith(name)).ToList(); 
 16 :         } 
 17 :         return null; 
 18 :     } 
 19 : } 
מחלקת המיפוי
מחלקת המיפוי היא המחלקה המקשרת בין מחלקת המודל לבין הטבלה המקבילה במסד הנתונים, מחלקת המיפוי מאפשרת לבצע CRUD (Create, Read, Update, Delete) מלא למידע הקיים בטבלה.



בשלב זה, מעבר לבנאי שמאתחל את התכונות נגדיר שלוש מתודות Select שונות:
 1  : public class WebsiteDB 
 2  : { 
 3  :      . . . 
 4  :      
 5  :     public Websites SelectAll() 
 6  :     { 
 7  :         . . . 
 8  :     } 
 9  :     public Websites SelectByName(string name) 
 10 :     { 
 11 :          . . . 
 12 :     } 
 13 :     public Website SelectByID(string id) 
 14 :     { 
 15 :         . . . 
 16 :     } 
 17 : } 
המתודה הראשונה מחזירה ממסד הנתונים את כל הרשומות המאוחסנות בטבלה Website, המתודה השנייה מחזירה את כל הרשומות על פי שם האתר, והשלישית את הרשומה לפי המזהה הייחודי שלה (שדה המפתח).

הקוד המלא של המחלקה:
 1  : public class WebsiteDB 
 2  : { 
 3  :     private string connectionString = @"Data Source=CORNER01\SQLEXPRESS;Initial Catalog=RssFeedDB;Integrated Security=True"; 
 4  :     private SqlConnection connection; 
 5  :     private SqlCommand command; 
 6  :     private SqlDataReader reader; 
 7  :     public WebsiteDB() 
 8  :     { 
 9  :         connection = new SqlConnection(connectionString); 
 10 :         command = new SqlCommand(); 
 11 :         command.Connection = connection; 
 12 :     } 
 13 :  
 14 :     public Websites SelectAll() 
 15 :     { 
 16 :         command.CommandText = "SELECT * FROM Website"; 
 17 :         reader = command.ExecuteReader(); 
 18 :         Websites websites = new Websites(); 
 19 :         try 
 20 :         { 
 21 :             command.Connection = connection; 
 22 :             connection.Open(); 
 23 :             Website website; 
 24 :             while (reader.Read()) 
 25 :             { 
 26 :                 website = new Website(); 
 27 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 28 :                 website.Name = reader["Name"].ToString(); 
 29 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 30 :                 website.Link = reader["Link"].ToString(); 
 31 :                 websites.Add(website); 
 32 :             } 
 33 :         } 
 34 :         catch (Exception e) 
 35 :         { 
 36 :  
 37 :         } 
 38 :         finally 
 39 :         { 
 40 :             if (reader != null) 
 41 :                 reader.Close(); 
 42 :  
 43 :             if (connection.State == ConnectionState.Open) 
 44 :                 connection.Close(); 
 45 :         } 
 46 :         return websites; 
 47 :     } 
 48 :     public Websites SelectByName(string name) 
 49 :     { 
 50 :         command.CommandText = string.Format("SELECT * FROM Website WHERE Name = {0}", name); 
 51 :         reader = command.ExecuteReader(); 
 52 :         Websites websites = new Websites(); 
 53 :         try 
 54 :         { 
 55 :             command.Connection = connection; 
 56 :             connection.Open(); 
 57 :             Website website; 
 58 :             while (reader.Read()) 
 59 :             { 
 60 :                 website = new Website(); 
 61 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 62 :                 website.Name = reader["Name"].ToString(); 
 63 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 64 :                 website.Link = reader["Link"].ToString(); 
 65 :                 websites.Add(website); 
 66 :             } 
 67 :         } 
 68 :         catch (Exception e) 
 69 :         { 
 70 :  
 71 :         } 
 72 :         finally 
 73 :         { 
 74 :             if (reader != null) 
 75 :                 reader.Close(); 
 76 :  
 77 :             if (connection.State == ConnectionState.Open) 
 78 :                 connection.Close(); 
 79 :         } 
 80 :         return websites; 
 81 :     } 
 82 :     public Website SelectByID(string id) 
 83 :     { 
 84 :         command.CommandText = string.Format("SELECT * FROM Website WHERE WebsiteID = {0}", id); 
 85 :         reader = command.ExecuteReader(); 
 86 :         Websites websites = new Websites(); 
 87 :         try 
 88 :         { 
 89 :             command.Connection = connection; 
 90 :             connection.Open(); 
 91 :             Website website; 
 92 :             while (reader.Read()) 
 93 :             { 
 94 :                 website = new Website(); 
 95 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 96 :                 website.Name = reader["Name"].ToString(); 
 97 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 98 :                 website.Link = reader["Link"].ToString(); 
 99 :                 websites.Add(website); 
 100:             } 
 101:         } 
 102:         catch (Exception e) 
 103:         { 
 104:  
 105:         } 
 106:         finally 
 107:         { 
 108:             if (reader != null) 
 109:                 reader.Close(); 
 110:  
 111:             if (connection.State == ConnectionState.Open) 
 112:                 connection.Close(); 
 113:         } 
 114:         if (websites.Count() == 1) 
 115:             return websites[0]; 
 116:         return null; 
 117:     } 
 118: } 
הסבר:
שורות 3-6 -
 הגדרת האובייקטים הניגשים למסד הנתונים כתכונות המחלקה WebsiteDB כדי שהם יהיו נגישים מכל המתודות.
שורה 7 - הבנאי של המחלקה, מקצה ומאתחל את האובייקטים.
שורה 14 - המתודה SelectAll קוראת ממסד הנתונים את כל הרשומות הנמצאות בטבלה Website.
שורה 16 - הגדרת השאילתה (SqlCommand.CommandText).
שורה 17 - אתחול ה- SqlDataReader על ידי העברת השאילתה.
שורה 18 - הגדרת מחלקת האוסף Websites אשר תכיל את כל האובייקטים המייצדים את הרשומות שיקראו ממסד הנתונים.
שורה 22 - התחברות למסד הנתונים.
שורה 24 - קריאת הרשומות אחת אחרי השניה בצורה סדרתית.
שורות 26-30 - בניית אובייקט Website המהווה "מראה" של רשומה בטבלה Website.
שורה 31 - הוספת האובייקט החדש לאוסף האובייקטים.
שורות 40-44 - התנתקות ממסד הנתונים במקטע finally אשר תמיד יתבצע גם אם תתרחש חריגה, מה שמבטיח שההתנתקות תמיד תבוצע.
שורה 46 - החזרת אוסף האובייקטים אשר נוצר בתהליך.

שורות 46-81 - המתודה SelectByName - התתליך הקריאה זהה לתהליך של המתודה SelectAll , השוני הוא רק בשאילתה (שורה 50)
שורות 82-117 - המתודה SelectByID
התתליך הקריאה זהה לתהליך של המתודה SelectAll , השוני הוא רק בשאילתה (שורה 84)

דוגמת קוד עבור שלב ראשון: RssReaderSample-ver 001





 



 
 



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