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";

ListBox 内容添加在下面

 /*
         * 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
4

1 回答 1

0

As with most programming problems, there are a million ways to do this. If you have control over the database design, you might consider having a blob or image column in which to store your list. You can take advantage of serialization in C# and stuff the raw bytes into that column. This would make querying for details of the list impossible from a sql query point of view. In regards to loading at startup, you'd grab the raw bytes from the column, deserialize them and attache them to the control of your form.

于 2012-12-05T13:11:40.300 回答