Insertion BD Sqlite avec valeur TextBox
Bonjour à tous
Je viens vous demander un peu d'aide pour insérer des éléments dans une base de donnée Sqlite.
J'ai réalisé mon projet avec MVVM Lite donc j'ai donc ma MainPage.xaml ci dessous :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
<Page
x:Class="Dictionary.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Dictionary"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid DataContext="{Binding Source={StaticResource Locator}, Path=Main}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBlock x:Name="Title" Text="Wiktionnary" FontSize="48" HorizontalAlignment="Center"></TextBlock>
<TextBox HorizontalAlignment="Center" Margin="45,30,45,0" Width="311"/>
<Button x:Name="Button_Add" Margin="44,90,45,0" HorizontalAlignment="Center" Width="311" Height="89" Content="Ajouter une définition" Click="Click1"></Button>
<Button x:Name="Button_List" HorizontalAlignment="Center" Margin="44,0,45,0" Height="89" Width="311" Content="Liste des définitions" Click="Click2"></Button>
</StackPanel>
</Grid>
</Page> |
ensuite de cette MainPage on va vers l'ajout d'une définition avec la page AddDefinition.xaml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<Page
x:Class="Dictionary.AddDefinition"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Dictionary"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid DataContext="{Binding Source={StaticResource Locator}, Path=Main}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBlock Text="Nom :" FontSize="24" HorizontalAlignment="Left" Margin="20,50,0,0"></TextBlock>
<TextBox x:Name="TextBox_Name" Margin="20,0,40,0" />
<TextBlock Text="Définition :" FontSize="24" Margin="20,40,0,0"></TextBlock>
<TextBox x:Name="TextBox_Def" Margin="20,0" Height="300"/>
<Button Command="{Binding SaveWord}" Content="Save" Margin="0,90,0,0" HorizontalAlignment="Center"></Button>
</StackPanel>
<TextBlock Text="{Binding Name}" FontSize="48" Margin="0,0,0,70"></TextBlock>
</Grid>
</Page> |
c'est de cette page que je veux récupérer le contenu des deux TextBox et l'insérer dans la base de donnée Sqlite
Comme j'utilise MVVM Lite j'ai tout dans le MainViewModel.cs :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using SQLite;
using Dictionary.Model;
namespace Dictionary.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
// L'attribut qui nous permettra de communiquer avec la base de données
private SQLiteAsyncConnection _connexion;
// Propriété bindable, pour afficher la liste des cartes de crédit sur l'interface
private ObservableCollection<Word> _word;
public ObservableCollection<Word> Word
{
get { return _word; }
set
{
_word = value;
RaisePropertyChanged(() => Word);
}
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
// Création de l'objet
_connexion = new SQLiteAsyncConnection("mybase.db");
InitializeDb();
}
/// <summary>
/// Supprime une carte de la base SQLite
/// </summary>
/// <param name="obj"></param>
async void InitializeDb()
{
// TODO : encapsuler les appels à Sqlite dans un try catch
await _connexion.CreateTableAsync<Word>();
// Insertion de données arbitraires
await InsertDatas();
// Récupération de toutes les données dans la base pour les insérer dans la collection bindée
IEnumerable<Word> words = await GetDatas();
Word = new ObservableCollection<Word>(words);
}
/// <summary>
/// Récupère l'ensemble des cartes de crédit de la base SQLite
/// </summary>
/// <returns></returns>
private async Task<IEnumerable<Word>> GetDatas()
{
return await _connexion.Table<Word>().ToListAsync();
}
/// <summary>
/// Insert des données arbitraires dans la base de données SQLite
/// </summary>
/// <returns></returns>
private async Task InsertDatas()
{
await _connexion.InsertAllAsync(new[]
{
new Word
{
Name = "Manger",
Definition = "Action de se nourrir"
},
new Word
{
Name = "Ecole",
Definition = "Pour s'instruire"
},
new Word
{
Name = "Pelle",
Definition = "Pour creuser"
}
});
}
}
} |
c'est dans la fin du code que je vois pas comment récupérer le contenu des TextBox pour le mettre à la place de Name et Definiton et insérer dans la base avec un bouton Enregistrer.
Voilà j'espère que vous m'avez compris merci de votre aide !