i have two table in sqlite i want to first compare the data from temperory table data with permanent table data then insert into it and also want to perform the delete function via uniquekey like if this if this key persent then it remove the data and it not it will add that , in one loop i want to perform the inseration and in second loop i want to perform the deletion and updation of data into permnent table of sqlite .
sent me code
thanks in advance .
log.Info("===into getUpdatedAndNewlyAddedRecord======= "); DataTable dataTable1 = new DataTable(); try { List table1 = new List(); using (var cmd = new SQLiteCommand(sqliteCon)) { var transaction = sqliteCon.BeginTransaction(IsolationLevel.ReadCommitted); try { log.Info("===into delete query for delete temperary table from sqlite ======= "); cmd.CommandText = deleteQuery; cmd.ExecuteNonQuery();
DataTable table = new DataTable(); DataTable dataTable = new DataTable(); SqlDataAdapter sqlAdapter = new SqlDataAdapter(); SqlDataReader sqlDataReader = null;
//PUSH all data into temp table from current query(sql server) to sqllite table which will be cleared each time using (SqlCommand sqlCommand = new SqlCommand(query, con)) { log.Info("===into sqlcommand query and sqlAdapter for sync vendor data from sql server======= "); // SqlDataAdapter sqlAdapter = new SqlDataAdapter(); sqlAdapter.SelectCommand = sqlCommand; sqlCommand.Parameters.Add(new SqlParameter("last_date", startDate.ToString("yyyy-MM-dd"))); sqlCommand.Parameters.Add(new SqlParameter("cur_date", endDate.ToString("yyyy-MM-dd"))); sqlDataReader = sqlCommand.ExecuteReader(); dataTable.Load(sqlDataReader);
log.Info("===going to fill the data from sqlAdapter into table======= "); sqlAdapter.AcceptChangesDuringFill = false; sqlAdapter.Fill(table); string jsonString = string.Empty; jsonString = JsonConvert.SerializeObject(table); log.InfoFormat("json output is geeting from sqlAdapter while it fill data in table {0}", jsonString);
}
using (var sqliteAdapter = new SQLiteDataAdapter(insertTempTableQuery, sqliteCon)) { log.Info("===into inserttemptablequery======= "); var cmdBuilder = new SQLiteCommandBuilder(sqliteAdapter); sqliteAdapter.Update(table); }
//get only updated and newly added rows for send it to server and push this to permannt table using (var sqlAdapter1 = new SQLiteDataAdapter(UpdatedAddedRecordsQuery, sqliteCon)) { log.Info("===updating the record first then insert below in permanent sqlite table ======= "); sqlAdapter1.AcceptChangesDuringFill = false; sqlAdapter1.Fill(dataTable1); }
using (var sqliteAdapter2 = new SQLiteDataAdapter(insertPermTableQuery, sqliteCon)) { log.Info("===into insertpermtablequery======= "); var cmdBuilder2 = new SQLiteCommandBuilder(sqliteAdapter2); sqliteAdapter2.Update(dataTable1); }
transaction.Commit(); } catch (Exception e) { try { log.Info("Transaction rollback " + e.Message); log.ErrorFormat(" complete exception for rollback is {0} " + e.StackTrace); transaction.Rollback(); } catch (Exception erol) { log.Error("isue in rollback" + erol.Message); log.ErrorFormat(" complete exception in rollback is {0} " + erol.StackTrace); } } } } catch (Exception ex) { log.Error("exception in getUpdatedAndNewlyAddedRecords is " + ex.ToString()); log.Info("===not using the getupdatedAddnewrecord function======= "); log.ErrorFormat(" complete exception in getUpdatedAndNewlyAddedRecords is {0} " + ex.StackTrace); } return dataTable1; }