Transfere les donnés dune bases acces dans un fichier txt

kmbmaster Messages postés 2 Date d'inscription jeudi 25 septembre 2003 Statut Membre Dernière intervention 16 octobre 2003 - 8 oct. 2003 à 11:04
gg00xiv Messages postés 656 Date d'inscription mercredi 22 août 2001 Statut Membre Dernière intervention 11 mars 2005 - 9 oct. 2003 à 13:35
bonjour ,je suis debutant en c# jaimerais pouvoir transfere les donnés dune base (accesss) dans un fichier texte mais je n'y arrive pas ! est ce qun peut m'aider SVP:
voici un code qui transpose les donnés via de srequetes SQL dans uune Listbox ,je voudrais qu' en meme temps le programme créé un fichier txt et sauvegarde le contenu des donnés de la base .
quel code ajouter?

FORM.CS
ou le placer dans mon programme ??
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace WindowsApplication3
{
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.ToolTip toolTip2;

private BaseAccess ar = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button8 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.toolTip2 = new System.Windows.Forms.ToolTip(this.components);
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(112, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(328, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "mybase.mdb";
this.textBox1.DoubleClick += new System.EventHandler(this.textBox1_DoubleClick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(744, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 3;
this.button1.Text = "Executer";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(112, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(608, 20);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "Fichier access :";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Name = "label2";
this.label2.TabIndex = 6;
this.label2.Text = "Ordre :";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 88);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(712, 212);
this.listBox1.TabIndex = 7;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button8,
this.button5,
this.button4,
this.button3,
this.button2,
this.button6,
this.button7});
this.groupBox1.Location = new System.Drawing.Point(744, 88);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(208, 224);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;
//
// button8
//
this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button8.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button8.Location = new System.Drawing.Point(112, 48);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(88, 23);
this.button8.TabIndex = 13;
this.button8.Text = "Supression";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button5
//
this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button5.Location = new System.Drawing.Point(8, 96);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(88, 32);
this.button5.TabIndex = 12;
this.button5.Text = "Projection colonnes";
this.toolTip1.SetToolTip(this.button5, "Renvoi les données des colonnes \'nom\' et \'handicape\' de la table \'Fournisseurs\'");
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button4.Location = new System.Drawing.Point(8, 152);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 23);
this.button4.TabIndex = 11;
this.button4.Text = "Noms colonnes";
this.toolTip1.SetToolTip(this.button4, "Renvoi les noms de chaque colonne présente dans la table \'Fournisseurs\'");
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button3.Location = new System.Drawing.Point(8, 16);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 23);
this.button3.TabIndex = 10;
this.button3.Text = "Projection table";
this.toolTip1.SetToolTip(this.button3, "Renvoi les données de toutes les colonnes de la table \'Fournisseurs\'");
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button2.Location = new System.Drawing.Point(8, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 23);
this.button2.TabIndex = 9;
this.button2.Text = "Projection colonne";
this.toolTip1.SetToolTip(this.button2, "Renvoi les données de la colonne \'nom\' de la table \'Fournisseurs\'");
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button6
//
this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button6.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button6.Location = new System.Drawing.Point(8, 184);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(88, 23);
this.button6.TabIndex = 9;
this.button6.Text = "Types colonnes";
this.toolTip1.SetToolTip(this.button6, "Renvoi pour chaque colonne de la table \'Fournisseurs\' son type de données");
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button7.Location = new System.Drawing.Point(112, 16);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(88, 24);
this.button7.TabIndex = 9;
this.button7.Text = "Insertion";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// toolTip2
//
this.toolTip2.ShowAlways = true;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(968, 323);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1,
this.listBox1,
this.label2,
this.label1,
this.textBox2,
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()

{
Application.Run(new Form1());
}

private void textBox1_DoubleClick(object sender, System.EventArgs e)
{
this.openFileDialog1.ShowDialog();
this.textBox1.Text = this.openFileDialog1.FileName;
}

private void button1_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

if (this.ar.Execute(this.textBox2.Text))
{
this.listBox1.Items.Clear();

ArrayList row;

while ((row = this.ar.Fetch()).Count > 0)
{
string srow = "";
foreach (BaseAccess.record rec in row)
{
srow += rec.column_name + "(" + rec.content + "),\t";
}
this.listBox1.Items.Add(srow);
}
}

this.ar.Close();
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.ar != null)
{
this.ar.Close();
}
}

private void button2_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList rows = this.ar.ProjectionColumn("Contact &' '& Ville","Fournisseurs");

if (rows.Count > 0)
{
this.listBox1.Items.Clear();

this.listBox1.Items.Add("Projection de la colonne 'Contact' et 'Ville' de la table 'Fournisseurs'");
foreach (string val in rows)
{
this.listBox1.Items.Add(val);
}
}

this.ar.Close();
}

private void button4_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList columns = this.ar.GetColumnsNames("Fournisseurs");

if (columns.Count > 0)
{
this.listBox1.Items.Clear();

string scolumn = "";
foreach (string column in columns)
{
scolumn += column + ",\t";
}
this.listBox1.Items.Add(scolumn);
}

this.ar.Close();
}

private void button3_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList rows = this.ar.ProjectionTable("Fournisseurs");

if (rows.Count > 0)
{
this.listBox1.Items.Clear();
this.listBox1.Items.Add("Projection de la table 'Fournisseurs'");
foreach (ArrayList row in rows)
{
string stmp = "";
foreach (BaseAccess.record record in row)
{
stmp += record.content + ",\t";
}
this.listBox1.Items.Add(stmp);
}
}

this.ar.Close();
}

private void button5_Click(object sender, System.EventArgs e)
{
string[] columns = new string[]{ "Contact","fax"};

this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList rows = this.ar.ProjectionColumns(columns,"Fournisseurs");

if (rows.Count > 0)
{
this.listBox1.Items.Clear();
this.listBox1.Items.Add("Projection des colonnes 'Contact' et 'fax' de la table 'Fournisseurs'");
foreach (ArrayList row in rows)
{
string stmp = "";
foreach (BaseAccess.record record in row)
{
stmp += record.content + ",\t";
}
this.listBox1.Items.Add(stmp);
}
}

this.ar.Close();
}

private void button6_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList types = this.ar.GetColumnsTypes("Fournisseurs");

if (types.Count > 0)
{
this.listBox1.Items.Clear();

foreach (BaseAccess.record rec in types)
{
this.listBox1.Items.Add(rec.column_name+"="+rec.content);
}
}

this.ar.Close();
}

private void button7_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList columns = new ArrayList();
columns.Add(new BaseAccess.record("Contact","gg00xiv"));
columns.Add(new BaseAccess.record("Ville",15000));
columns.Add(new BaseAccess.record("fax",false));

this.listBox1.Items.Clear();
if (this.ar.InsertRow(columns,"Fournisseurs"))
this.listBox1.Items.Add("Insertion reussite");
else
this.listBox1.Items.Add("Insertion echouée");

this.ar.Close();
}

private void button8_Click(object sender, System.EventArgs e)
{
this.ar = new BaseAccess(this.textBox1.Text);
this.ar.Open();

ArrayList columns_id = new ArrayList();
columns_id.Add(0);
columns_id.Add(3);

this.listBox1.Items.Clear();
if (this.ar.DeleteRows(columns_id,"Fournisseurs"))
this.listBox1.Items.Add("Supression reussite");
else
this.listBox1.Items.Add("Supression echouée");

this.ar.Close();
}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//FileWrite fw = new FileWrite("c:\\fichier.txt");

}
}
}

Basesaccess.CS

using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace WindowsApplication3
{
public class BaseAccess
{
private string filename;
private string connStr;
private OleDbConnection oConn;
private OleDbDataReader rdr;
private OleDbCommand oCmd;

// type record contenant le nom d'une colonne et sa valeur associée
public struct record
{
public record(string column_name, object content)
{
this.column_name = column_name;
this.content = content;
}

public string column_name;
public object content;
}

// constructeur nécessitant le path du fichier base de donnée Access
public BaseAccess(string filename)
{
this.filename = filename;
this.connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename;
}

// établie une connexion sur la base Access
public bool Open()
{
try
{
this.oConn = new OleDbConnection(this.connStr);
this.oConn.Open();
return true;
}
catch
{
return false;
}
}

// ferme la connexion à la base Access
public bool Close()
{
try
{
this.oConn.Close();
return true;
}
catch
{
return false;
}
}

// execute une requête SQL sur la base Access
public bool Execute(string ordre)
{
try
{
this.oCmd = new OleDbCommand(ordre,this.oConn);
this.rdr = this.oCmd.ExecuteReader();
return true;
}
catch
{
return false;
}
}

// renvoi une ArrayList contenant des enregistrements de type record
// contenant les différents champs de la ligne lue
public ArrayList Fetch()
{
ArrayList row = new ArrayList();

if (this.rdr.Read())
{
record rec;

for (int i=0;i<this.rdr.FieldCount;i++)
{
rec.column_name = this.rdr.GetName(i);
rec.content = this.rdr[i];
row.Add(rec);
}
}

return row;
}

// renvoi une ArrayList contenant des string correspondantes aux valeurs
// dans la colonne spécifiée
public ArrayList ProjectionColumn(string column_name,string table_name)
{
ArrayList projection = new ArrayList();

if (this.Execute("SELECT "+column_name+" FROM "+table_name))
{
ArrayList row;

while ((row = this.Fetch()).Count > 0)
{
projection.Add(((record)row[0]).content);
}
}

return projection;
}

// renvoi une ArrayList contenant des string correspondantes aux différents noms
// de colonnes de la table passée en paramètre
public ArrayList GetColumnsNames(string table_name)
{
ArrayList columns = new ArrayList();

if (this.Execute("SELECT * FROM "+table_name))
{
for (int i=0;i<this.rdr.FieldCount;i++)
{
columns.Add(this.rdr.GetName(i));
}
}

return columns;
}

// renvoi une ArrayList contenant des ArrayList contenant chacune les différents
// champs des colonnes de la table spécidiée en paramètre
public ArrayList ProjectionTable(string table_name)
{
ArrayList rows = new ArrayList();

if (this.Execute("SELECT * FROM "+table_name))
{
ArrayList row;
while ((row = this.Fetch()).Count > 0)
{
rows.Add(row);
}
}

return rows;
}

// renvoi une ArrayList contenant des ArrayList contenant chacune les différents
// champs des colonnes passées en paramètre de la table spécidiée
public ArrayList ProjectionColumns(string[] columns_name,string table_name)
{
ArrayList rows = new ArrayList();

if (this.Execute("SELECT "+string.Join(",",columns_name)+" FROM "+table_name))
{
ArrayList row;
while ((row = this.Fetch()).Count > 0)
{
rows.Add(row);
}
}

return rows;
}

// renvoi les types des colonnes d'une table
public ArrayList GetColumnsTypes(string table_name)
{
ArrayList types = new ArrayList();
record rec;

if (this.Execute("SELECT * FROM "+table_name))
{
for (int i=0;i<this.rdr.FieldCount;i++)
{
rec.column_name = this.rdr.GetName(i);
rec.content = this.rdr.GetFieldType(i).ToString();
types.Add(rec);
}
}

return types;
}

// insert une line dans une table
public bool InsertRow(ArrayList columns, string table_name)
{
string order = "INSERT INTO "+table_name+"(";
ArrayList columns_values = new ArrayList();

foreach (record rec in columns)
{
// on rajoute les colonnes a remplir
order += rec.column_name + ",";
// on fait la liste des valeurs a ajouter
columns_values.Add(rec.content);
}

// on supprime le virgule a la fin
order = order.Substring(0,order.Length-1);
order += ") VALUES(";

foreach (object val in columns_values)
{
if (val is string || val is DateTime)
{
order += "'" + val + "',";
}
else
{
order += val + ",";
}
}

order = order.Substring(0,order.Length-1);
order += ")";

Console.Out.WriteLine("tentative de soumission : "+order);
return this.Execute(order);
}

public bool DeleteRows(ArrayList rows_id, string table_name)
{
return false;
}
}
}

1 réponse

gg00xiv Messages postés 656 Date d'inscription mercredi 22 août 2001 Statut Membre Dernière intervention 11 mars 2005 13
9 oct. 2003 à 13:35
trop de lignes dans ton post, ca donne mal au crane .

ceci mis a part : utilise la classe StreamWriter(filename)

tu parcours les differentes DataRow de ta DataTable et tu ecrit chaque row avec un:
tonStreamWriter.WriteLine(row[0]+";"+row[1]+";"+...);
0
Rejoignez-nous