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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
namespace WpfListViewDataTable
{
/// <summary>
/// Logique d'interaction pour WinLoadTableArbre.xaml
/// </summary>
public partial class WinLoadTableArbre : Window
{
List<Arbre> arbres = new List<Arbre>();
public WinLoadTableArbre()
{
InitializeComponent();
}
// => FolderTemp est un dossier temporaire ou sont stockes
// les images "blob" de la table apres lecture...
string pathFolder = string.Empty;
string appPath = Directory.GetCurrentDirectory() + @"\";
private void btnLoadTableArbre_Click(object sender, RoutedEventArgs e)
{
string cnStr = GetConnectionString();
// cree FolderTemp .
pathFolder = appPath + @"\FolderTemp\";
if (!Directory.Exists(pathFolder))
{
Directory.CreateDirectory(pathFolder);
}
LoadTable(cnStr);
}
private void LoadTable(string connectionString)
{
try
{
using (SqlCeConnection cnx = new SqlCeConnection(connectionString))
{
cnx.Open();
using (SqlCeCommand cmd = new SqlCeCommand("SELECT ID, Nom, Age, BitmapImage,Description, Comes FROM Arbre", cnx))
{
FileStream stream; // => pour ecrire BLOB dans un file (*.bmp) dans TempFolder.
BinaryWriter writer; // =>pour "streamer" le BLOB dans FileStream .
int bufferSize = 100; // taille buffer BLOB .
byte[] outByte = new byte[bufferSize]; // buffer lecture outByte[] remplit par GetBytes.
long retval=0; // nb bytes renvoye par GetBytes.
long startIndex = 0; // position depart dans la sortie .
int tempID = 0;
string tempNom = "";
int tempAge = 0;
string tempUriImage = "";
string tempDesc = "";
string tempComes = "";
SqlCeDataReader rd = cmd.ExecuteReader(CommandBehavior.SingleResult);
while (rd.Read())
{
// lit champs en sequence(ordre) dans table.
tempID = rd.GetInt32(0);
tempNom = rd.GetString(1);
tempAge = rd.GetInt32(2);
// cree un nouveau file bitmap de sortie.
stream = new FileStream(
pathFolder + "photo" + tempID.ToString() + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(stream);
// byte depart => nouveau record BLOB.
startIndex = 0;
// lit dans outByte[] & store nbre de bytes lus.
//ATTENTION : ce "3" est le numero record du champ BitmapImage dans table BD
retval = rd.GetBytes(3, startIndex, outByte, 0, bufferSize);
// tant que
while (retval == bufferSize)
{
writer.Write(outByte);
writer.Flush();
// Repositionne index depart à derniere sortie.
startIndex += bufferSize;
//ATTENTION : au "3"
retval = rd.GetBytes(3, startIndex, outByte, 0, bufferSize);
}
// ecrit le restant du buffer.
writer.Write(outByte, 0, (int)retval - 1);
writer.Flush();
// Ferme stream.
writer.Close();
stream.Close();
//Lit la suite des champs
tempDesc = rd.GetString(4);
tempComes=rd.GetString(5);
//recupere uri de l'image ...pour la donner à BitmapImage
tempUriImage = pathFolder + "photo" + tempID.ToString() + ".bmp";
arbres.Add(new Arbre() {
ID = tempID ,
Nom = tempNom ,
Age = tempAge ,
BitmapImage = new BitmapImage(new Uri(tempUriImage, UriKind.RelativeOrAbsolute)),
Desc = tempDesc ,
Comes = tempComes});
}
listViewDonne.ItemsSource = arbres;
rd.Close();
}
cnx.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private static string GetConnectionString()
{
return Properties.Settings.Default.ArbreBDCnStr;
}
}
} |
Partager