Exemple #1
0
 //provide interface for controller
 public static int SaveTask(Mission item)
 {
     return(MissionDaoBridges.SaveTask(item));
 }
        //save mission in database;
        public int SaveItem(Mission item)
        {
            int r;

            lock (locker) {
                //if it is a mission that already exsist, then just update it.
                if (item.ID != 0)
                {
                    connection = new SqliteConnection("Data Source=" + path);
                    connection.Open();
                    using (var command = connection.CreateCommand()) {
                        command.CommandText = "UPDATE [Items] SET [Name] = ?, [Description] = ?, [Deadline] = ?, [Done] = ? WHERE [_id] = ?;";
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = item.Name
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = item.Description
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = item.Deadline
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.Int32)
                        {
                            Value = item.Done
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.Int32)
                        {
                            Value = item.ID
                        });
                        r = command.ExecuteNonQuery();
                    }
                    connection.Close();
                    return(r);
                }
                else                    // if it is a new mission, then create a new record in database
                {
                    connection = new SqliteConnection("Data Source=" + path);
                    connection.Open();
                    using (var command = connection.CreateCommand()) {
                        command.CommandText = "INSERT INTO [Items] ([Name], [Description], [Deadline], [Done]) VALUES (? ,?, ?, ?)";
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = item.Name
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = item.Description
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.String)
                        {
                            Value = item.Deadline
                        });
                        command.Parameters.Add(new SqliteParameter(DbType.Int32)
                        {
                            Value = item.Done
                        });
                        r = command.ExecuteNonQuery();
                    }
                    connection.Close();
                    return(r);
                }
            }
        }