in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

SQLiteDataAdapter.Update returns 0 (does not insert data into the database)

Last post 04-28-2010 3:33 PM by lirik. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 04-26-2010 10:33 PM

    • lirik
    • Not Ranked
    • Joined on 04-26-2010
    • Posts 2

    SQLiteDataAdapter.Update returns 0 (does not insert data into the database)

    Hi everybody! I've been trying to implement a CSV insert with a little trick: I use the OleDb CSV parser to fill my SQLiteDataSet, then I want to use the SQLiteDataSet to update the SQLite database. Unfortunately I've been stuck on this one problem- when I call Update on the SQLiteDataAdapter it always returns 0. For some reason the HasChanged() flag returns false too.

    static void Main(string[ args)
    {
    SQLiteDatabase target = new SQLiteDatabase();
    string csvFileName = "D:\\YahooTagsInfo.csv";
    string tableName = "Tags";
    target.InsertData(csvFileName, tableName);
    Console.ReadKey();
    }

    public void InsertData(String csvFileName, String tableName)
    {
    String dir = Path.GetDirectoryName(csvFileName);
    String name = Path.GetFileName(csvFileName);

    using (OleDbConnection conn =
    new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
    dir + ";Extended Properties=""Text;HDR=Yes;FMT=Delimited\""))
    {
    using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + name, conn))
    {
    conn.Open();
    QuoteDataSet ds = new QuoteDataSet();
    adapter.Fill(ds, tableName);
    Console.WriteLine("Num rows loaded is " + ds.Tags.Rows.Count);
    InsertData(ds, tableName);
    }
    }
    }

    public void InsertData(QuoteDataSet data, String tableName)
    {
    using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
    {
    conn.Open();
    using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, conn))
    {
    using (sqliteAdapter.InsertCommand = new SQLiteCommandBuilder(sqliteAdapter).GetInsertCommand())
    //using (sqliteAdapter.UpdateCommand = new SQLiteCommandBuilder(sqliteAdapter).GetUpdateCommand())
    {
    Console.WriteLine("Insert Command: " + sqliteAdapter.InsertCommand.CommandText);
    Console.WriteLine("Param count: " + sqliteAdapter.InsertCommand.Parameters.Count);
    /*
    Console.WriteLine("Update Command: " + sqliteAdapter.UpdateCommand.CommandText);
    Console.WriteLine("Param count: " + sqliteAdapter.UpdateCommand.Parameters.Count);
    */
    Console.WriteLine("Has changes: " + data.HasChanges());
    Console.WriteLine("Num rows updated is " +
    sqliteAdapter.Update(data, tableName));
    }
    }
    }
    }

    Here is the actual output:

    Num rows loaded is 83
    Insert Command: INSERT INTO [Tags] ([tagId], [tagName], [description], [colName]
    , [dataType], [realTime]) VALUES (@param1, @param2, @param3, @param4, @param5, @
    param6)
    Param count: 6
    Has changes: False
    Num rows updated is 0

    Could anybody help me figure out what's wrong here?

    Here is a pastie link with my SQLiteDataSet (data) in xml format.

  • 04-28-2010 3:33 PM In reply to

    • lirik
    • Not Ranked
    • Joined on 04-26-2010
    • Posts 2

    Re: SQLiteDataAdapter.Update returns 0 (does not insert data into the database)

    I found out the real problem and I posted it along with my question on stack overflow: http://stackoverflow.com/questions/2711021/sqlitedataadapter-update-method-returning-0

    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.

     

     An even better (and more elegant) solution is to set the AcceptChangesDuringFill flag to false:

        using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + name, conn))
       
    {
           
    // this is the proper way to transfer rows
            adapter
    .AcceptChangesDuringFill = false;

           
    QuoteDataSet ds = new QuoteDataSet();
            adapter
    .Fill(ds, tableName);
           
    Console.WriteLine("Num rows loaded is " + ds.Tags.Rows.Count);
           
    InsertData(ds, tableName);
       
    }

     

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems