Sample data for DataTable for testing – 我的DataTable 的 資料樣本

為了方便自己測候和示範一些ASP.Net MVC/ ASP.Net/ WinForm/WPF/WCF 功能
我終於寫了一2個簡單的Classes 和一些Code 去建立一個 Dummy 的DataTable/DataSet,和一些小小的功能幫助取資料

第一個ClassFood Category 用來把食物分類的
C#

 public class FoodCategory
    {
	    public string FoodCategory_ID {get;set;}
	    public string FoodCategory_Name {get;set;}
    }

另一個Class食物 來定義食的名和那一個類等等

C#

public class Food
    {
        public string Food_ID { get; set; }
        public string Food_Name { get; set; }
        public decimal Price { get; set; }
        public string FoodCategory_ID{get;set;}
     }

之後我便定義了一個Helper Class
用來方便自己取資料的
由於最終的目的是用來simulate 連接database
最出資料到DataTable上的動作..
[因為我常常感到..當我要測試一些資料時
需要連接到資料庫取資料是一件得麻煩的是..
因為在電腦上我沒有自己啟用MSSQL Server
還有.即使連接了SQL Server
我們還是要寫一些SQL 來取資料..
所以便有了這個想法去建立一個有小小Relational 的Dummy DataTable了]

以下是我所建立的Helper class 的所有的程式碼
C#

 public class ShareChiWai_Model
    {
//這可以取得所有Food的資料放在DataTable上
        public DataTable Get_Food_Tbl()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add(new DataColumn("Food_ID", Type.GetType("System.String")));
            tbl.Columns.Add(new DataColumn("Food_Name", Type.GetType("System.String")));
            tbl.Columns.Add(new DataColumn("Price", Type.GetType("System.Decimal")));
            tbl.Columns.Add(new DataColumn("FoodCategory_ID", Type.GetType("System.String")));

            DataRow dr1 = tbl.NewRow();
            dr1["Food_ID"] = "00001";
            dr1["Food_Name"] = "Apple";
            dr1["Price"] = 0.99;
            dr1["FoodCategory_ID"] = "00001";
            tbl.Rows.Add(dr1);

            DataRow dr2 = tbl.NewRow();
            dr2["Food_ID"] = "00002";
            dr2["Food_Name"] = "American bison";
            dr2["Price"] = 7.99;
            dr2["FoodCategory_ID"] = "00002";
            tbl.Rows.Add(dr2);

            tbl.Rows.Add(new Object[] { "00003", "Asparagus", 3.99, "00003" });
            tbl.Rows.Add(new Object[] { "00004", "Banana", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00005", "Beef", 1.99, "00002" });
            tbl.Rows.Add(new Object[] { "00006", "Broccoli", 1.09, "00003" });
            tbl.Rows.Add(new Object[] { "00007", "Cherry", 1.99, "00001" });
            //start Vegetable
            tbl.Rows.Add(new Object[] { "00008", "Yams", 1.99, "00003" });
            tbl.Rows.Add(new Object[] { "00009", "Sweet potatoes", 5.99, "00003" });
            tbl.Rows.Add(new Object[] { "00010", "Olives", 5.99, "00003" });
            tbl.Rows.Add(new Object[] { "00011", "Tomato", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00012", "Spinach", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00013", "Radish", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00014", "Pumpkin", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00015", "Onion", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00016", "Nopales ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00017", "Mushrooms ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00018", "Leeks ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00019", "Kohlrabi ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00020", "Japanese Eggplant ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00021", "Hot Pepper", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00022", "Garlic", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00023", "Fordhooks ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00024", "Eggplant ", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00025", "Dinosaur Kale", 0.99, "00003" });
            tbl.Rows.Add(new Object[] { "00026", "Cabbage ", 0.99, "00003" });
            //End Vegetable
            tbl.Rows.Add(new Object[] { "00027", "Cattle", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00028", "Duck", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00029", "E -- Unknown", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00030", "Fugu", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00031", "Guinea pig", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00032", "Zebra", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00033", "Whale", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00034", "Tuna", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00035", "Salmon", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00036", "Pork", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00037", "Orange Roughy", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00038", "Milkfish", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00039", "Kingfish", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00040", "Watermelon", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00041", "Strawberries", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00042", "Raspberries", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00043", "Raisins", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00044", "Prunes", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00045", "Plums", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00046", "Haddock", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00047", "Nutria", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00048", "Lamb", 0.99, "00002" });
            tbl.Rows.Add(new Object[] { "00049", "Iguana", 0.99, "00002" });

            //fruit continue
            tbl.Rows.Add(new Object[] { "00050", "Orange", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00051", "Mango", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00052", "Kiwi", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00053", "Lime", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00054", "Jackfruit", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00055", "Guava", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00056", "Grapefruit", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00057", "Fig", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00058", "Durian", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00059", "Papaya", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00060", "Ugli Fruit", 0.99, "00001" });
            tbl.Rows.Add(new Object[] { "00061", "Tamarillo", 0.99, "00001" });
            //Fruit End

            return tbl;
        }

//這可以取得Food Categories 的資料放在DataTable上
        public DataTable Get_Food_Categories()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add(new DataColumn("FoodCategory_ID", Type.GetType("System.String")));
            tbl.Columns.Add(new DataColumn("FoodCategory_Name", Type.GetType("System.String")));

            DataRow dr1 = tbl.NewRow();
            dr1["FoodCategory_ID"] = "00001";
            dr1["FoodCategory_Name"] = "Fruit";
            tbl.Rows.Add(dr1);

            DataRow dr2 = tbl.NewRow();
            dr2["FoodCategory_ID"] = "00002";
            dr2["FoodCategory_Name"] = "Meat/Animal";
            tbl.Rows.Add(dr2);
            DataRow dr3 = tbl.NewRow();
            dr3["FoodCategory_ID"] = "00003";
            dr3["FoodCategory_Name"] = "Vegetable";
            tbl.Rows.Add(dr3);

            return tbl;
        }
//這可以取得在指定的CategoryID資訊
        public DataTable Get_CategoryByID(string FoodCategory_ID)
        {

            DataTable tbl = new DataTable();
            DataRow[] dr_Array = Get_Food_Categories().Select("FoodCategory_ID ='" + FoodCategory_ID + "'");
            if (dr_Array.Length > 0)
            {
                tbl = dr_Array[0].Table.Copy();
                tbl.Clear();
            }

            foreach (DataRow dr in dr_Array)
            {
                tbl.ImportRow(dr);
            }
            return tbl;
        }
//這可以取得在指定的CategoryID內的食物資料
        public DataTable Get_FoodByCategoryID(string FoodCategory_ID)
        {

            DataTable tbl = new DataTable();
            DataRow[] dr_Array =Get_Food_Tbl().Select("FoodCategory_ID ='" + FoodCategory_ID + "'");
            if (dr_Array.Length > 0)
            {
                tbl = dr_Array[0].Table.Copy();
                tbl.Clear();
            }

            foreach (DataRow dr in dr_Array)
            {
                tbl.ImportRow(dr);
            }
            return tbl;
        }

//這可以取得在指定的食物資料
        public DataTable Get_FoodByID(string Food_ID)
        {

            DataTable tbl = new DataTable();
            DataRow[] dr_Array = Get_Food_Tbl().Select("Food_ID ='" + Food_ID + "'");
            if (dr_Array.Length > 0)
            {
                tbl = dr_Array[0].Table.Copy();
                tbl.Clear();
            }

            foreach (DataRow dr in dr_Array)
            {
                tbl.ImportRow(dr);
            }
            return tbl;
        }
    }
//Food Category Class
    public class FoodCategory
    {
        public string FoodCategory_ID { get; set; }
        public string FoodCategory_Name { get; set; }
    }
//Food Class
    public class Food
    {
        public string Food_ID { get; set; }
        public string Food_Name { get; set; }
        public decimal Price { get; set; }
        public string FoodCategory_ID { get; set; }
    }

之後我們便可以運用個Helper class 來取得一些我們可以用的測試資料了
E.G.

ShareChiWai_Model Share_ChiWai = new ShareChiWai_Model();
DataTable tbl_FoodCategories = Share_ChiWai.Get_Food_Categories(); //這可以取得Food Categories 的資料放在DataTable上

DataTable tbl_Foods = Share_ChiWai.Get_Food_Tbl();//這可以取得所有Food的資料放在DataTable上

DataTable tbl_CategoryByID =Share_ChiWai.Get_CategoryByID("00001"); //這可以取得在指定的CategoryID資訊

DataTable tbl_FoodByID =Share_ChiWai.Get_FoodByID("00008");//這可以取得在指定的食物資料

DataTable tbl_FoodByCategoryID =Share_ChiWai.Get_FoodByCategoryID("00001"); //這可以取得在指定的CategoryID內的食物資料


將來再次加入更多的有用功能

Hope you find it useful

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.