2012-12-05 123 views
0

我有兩個列表和一個列表框,這些列表都是他們自己的類,並且列表框位於Form1上。每個列表通過一個帶有相應名稱的GUI添加到該拾取列表的FrmPickups和該列表的FrmVisits。數據庫中的持久性

我的問題是我該如何將數據添加到列表中,並將其存儲到數據庫以在項目啓動時重新加載,以及如何在啓動時將其加載到列表框中。

部分代碼如下:

數據添加到列表如下:

 theVisit.name = txtName.Text; 
     theVisit.address = txtAddress.Text; 
     theVisit.arrival = DateTime.Parse(txtArrival.Text); 
     theVisit.Lat = Double.Parse(txtLat.Text); 
     theVisit.Lon1 = Double.Parse(txtLong.Text); 
     theVisit.type = "Visit"; 

列表框的內容在下面

/* 
     * Update the list on this form the reflect the visits in the list 
     */ 
     lstVisits.Items.Clear(); 
     //Clear all the existing visits from the list 

     List<String> listOfVis = theList.listVisits(); 
     //Get a list of strings to display in the list box 

     List<String> listOfpic = thePickup.listPickups(); 
     //Get a list of strings to display in the list box 

     lstVisits.Items.AddRange(listOfVis.ToArray()); 
     //Add the strings to the listBox. Note to add a list of strings in one go we have to 
     //use AddRange and we have to use the ToArray() method of the list that we are adding 

     lstVisits.Items.AddRange(listOfpic.ToArray()); 
     //Add the strings to the listBox. Note to add a list of strings in one go we have to 
     //use AddRange and we have to use the ToArray() method of the list that we are adding 
+0

你的數據訪問層是什麼樣的? –

+0

@ DanielA.White我甚至不知道那是什麼...,這是我申請大學一年級的唯一原因,但是,我是一個總noobie – TAM

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

與大多數編程問題增加,也有一百萬種方法來做到這一點。如果您可以控制數據庫設計,則可以考慮在其中存儲您的列表的blob或image列。您可以利用C#中的序列化並將原始字節填充到該列中。這將從sql查詢的角度查詢不可能的列表的細節。 關於在啓動時加載,您可以從列中獲取原始字節,反序列化它們並將它們附加到表單的控件中。