Use DataSet for retrieving, updating and inserting

2020-08-05 01:12发布

问题:

On visual studio I can create a DataSet of my SQLite database by doing:

1) Connect to sqlite database file and create a table

2) Add new DataSet to my solution (DataSet2.xsd)

3) Drag tables to the designer


Now how can I make use of the objects that visual studio created for me?

for example I am trying to do something like:

        DataSet2.TableTestDataTable t = new DataSet2.TableTestDataTable();
        var objects = t.GetObjectData(..// do not know how to use it

OR

        DataSet2TableAdapters.TableTestTableAdapter t = new DataSet2TableAdapters.TableTestTableAdapter();
        var data = t.GetData(); 

        var q = from a in data
                select a;

        foreach (DataSet2.TableTestRow row in q)
        {
            row.integer = 12345; // change value of column integer               
        }
        t.submitchanges(); // method does note exists

回答1:

    DataSet2TableAdapters.TableTestTableAdapter t = new DataSet2TableAdapters.TableTestTableAdapter();

    DataSet2 ds = new DataSet2();
    t.Fill(ds.TableTest); 

    foreach (DataSet2.TableTestRow row in ds.TableTest.Rows)
    {
        row.integer = 12345; // change value of column integer               
    }

    t.Update(ds2);

Something like that ....



回答2:

Try:

var q = (from a in data
        select a).ToArray();

And instead of t.SubmitChanges(); use t.Update(q);.