SQLiteDataAdapter Update method returning 0

后端 未结 2 955
独厮守ぢ
独厮守ぢ 2021-01-06 06:13

I loaded 83 rows from my CSV file, but when I try to update the SQLite database I get 0 rows... I can\'t figure out what I\'m doing wrong.

The program outputs:

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 06:59

    I was able to get around the issue by going through each row and changing it's state by calling SetAdded();... after I did that the Update command worked like a charm.

    public void InsertData(QuoteDataSet dataSet, String tableName)
    {
        int numRowsUpdated = 0;
        using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
        {
            conn.Open();
            using (SQLiteTransaction transaction = conn.BeginTransaction())
            using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, conn))
            {
                using (sqliteAdapter.InsertCommand = new SQLiteCommandBuilder(sqliteAdapter).GetInsertCommand())
                {
                    var rows = dataSet.Tags.AsEnumerable();
                    foreach (var row in rows)
                    {
                        row.SetAdded();
                    }
                    numRowsUpdated = sqliteAdapter.Update(dataSet, tableName);
                }
                transaction.Commit();
            }
        }
        Console.WriteLine("Num rows updated is " + numRowsUpdated);
    }
    

    I assume that when the DataSet is filled from the CSV file and I then I attempt to call Update in order to insert the data into the database, the state of the row does not indicate any changes. We have to tell the DataAdapter that there is a change in the DataSet because all it sees is that are no changes to the DataSet with respect to the CSV file it was populated from and it doesn't realize that these are brand new rows for the database I'm trying to put the data in.

提交回复
热议问题