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
|
using System;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;
using System.Data;
namespace TEST_SQLite
{
public partial class Form1 : Form
{
private DataSet artcollectionDataSet;
private DataTable dtItems;
private SQLiteConnection connexion = null; // << Changé pour SQLite
private SQLiteDataAdapter itemsTableAdapter; // << Changé pour SQLite
private SQLiteCommandBuilder builderItems; // << Changé pour SQLite
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.artcollectionDataSet = new DataSet();
connexion = CreateConnection();
Init_Dataset_Items();
}
static SQLiteConnection CreateConnection()
{
string MonApplipath = Directory.GetCurrentDirectory();
SQLiteConnection connexion;
// Create a new database connection:
connexion = new SQLiteConnection("Data Source=" + MonApplipath +"\\artcollection.db; Version = 3");
// Open the connection:
try
{
connexion.Open();
}
catch (Exception ex)
{
}
return connexion;
}
private void Init_Dataset_Items()
{
if (connexion == null) return;
if (dtItems != null) artcollectionDataSet.Tables.Remove(dtItems);
dtItems = new DataTable("Items");
artcollectionDataSet.Tables.Add(dtItems);
string strSelectItems = "SELECT * FROM Items ";
itemsTableAdapter = new SQLiteDataAdapter(strSelectItems, connexion);
builderItems = new SQLiteCommandBuilder();
builderItems.DataAdapter = itemsTableAdapter;
builderItems.GetUpdateCommand();
builderItems.GetInsertCommand();
builderItems.GetDeleteCommand();
itemsTableAdapter.Fill(artcollectionDataSet, dtItems.TableName);
}
private void SQL_Charge_Dataset_Items(int KeyCat)
{
Console.WriteLine("SQL_Charge_Dataset_Items");
if (connexion == null) return;
if (dtItems != null) artcollectionDataSet.Tables.Remove(dtItems);
dtItems = new DataTable("Items");
artcollectionDataSet.Tables.Add(dtItems);
// Create the SelectCommand.
SQLiteCommand commandItems = new SQLiteCommand("SELECT * FROM Items " +
"WHERE REF = @REF", connexion);
commandItems.Parameters.Add("@REF", DbType.Int32);
commandItems.Parameters["@REF"].Value = KeyCat;
itemsTableAdapter.SelectCommand = commandItems;
builderItems = new SQLiteCommandBuilder();
builderItems.DataAdapter = itemsTableAdapter;
builderItems.GetUpdateCommand();
builderItems.GetInsertCommand();
builderItems.GetDeleteCommand();
itemsTableAdapter.Fill(artcollectionDataSet, dtItems.TableName);
connexion.Close(); // << Ajouté Close() avec SQLite
}
private void button1_Click(object sender, EventArgs e)
{
var Key = Int32.Parse(textBox1.Text);
SQL_Charge_Dataset_Items(Key);
label1.Text = artcollectionDataSet.Tables["Items"].Rows[0]["Titre"].ToString().Trim();
}
}
} |