Connection base

Résolu
cs_fayrous Messages postés 48 Date d'inscription lundi 23 février 2009 Statut Membre Dernière intervention 30 avril 2009 - 20 mars 2009 à 14:21
cs_fayrous Messages postés 48 Date d'inscription lundi 23 février 2009 Statut Membre Dernière intervention 30 avril 2009 - 20 mars 2009 à 16:02
Salut,
voila mon code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\Administrateur\\Bureau\\baseeffacer\\qcmm.mdb";
OleDbConnection con = new OleDbConnection(connection);
con.Open();
OleDbCommand com;
com = new OleDbCommand("insert into Etudiant values (@n,@v,@ad)", con);

com.Parameters.Add("@n", OleDbType.WChar).Value = nom.Text;
com.Parameters.Add("@v", OleDbType.Integer).Value = Convert.ToInt32(textBox3.Text);
com.Parameters.Add("@ad", OleDbType.WChar).Value = prénom.Text;


try
{

com.ExecuteNonQuery();
MessageBox.Show("OK");
}
catch
{
con.Close();
}
}

}
quand je fais remplir le formulaire , les données ne s'enrregistre pas dans la base .mon table étudiant est vide.
S.V.P
pouvez vous me dire ou est l'erreur.

et merci d'avance

3 réponses

billou_13 Messages postés 860 Date d'inscription jeudi 4 mars 2004 Statut Membre Dernière intervention 19 août 2014 29
20 mars 2009 à 14:56
Par contre, je n'aurais pas mis les messagebox (autant au niveau OK que Erreur) dans le code mais plutôt en dehors.
J'aurais tout mis dans une fonction (en enlevant carrément le catch) [Exemple: public void InsertEtudiant(string lastname, int age, string firstname) { ... } ]
Et j'aurai appeler cette fonction comme ceci:
try
{
  InsertEtudiant(lastname, age, firstname);
}
catch(Exception ex)
{
  MessageBox.Show(string.Format("Erreur. Message: '{0}'", ex.Message));
}

Billou_13
Bask En Force

--------------------------------------------------------------------
Connaître la réponse est une chose, savoir pourquoi en est une autre
---------------------
3
billou_13 Messages postés 860 Date d'inscription jeudi 4 mars 2004 Statut Membre Dernière intervention 19 août 2014 29
20 mars 2009 à 14:52
Bonjour,

Un petit problème dans ton code, tu ne fermes ta connexion que s'il y a un erreur. Cela est embêtant.
Voici comment j'aurais vu ton code:
string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\Administrateur\\Bureau\\baseeffacer\\qcmm.mdb";
using (OleDbConnection con = new OleDbConnection(connection))
{
  try
  {
    //Ouverture de la connexion
    con.Open();

    using (OleDbCommand com = new OleDbCommand("insert into Etudiant values (@n,@v,@ad)", con))
    {
      com.Parameters.Add("@n", OleDbType.WChar).Value = nom.Text;
      com.Parameters.Add("@v", OleDbType.Integer).Value = Convert.ToInt32(textBox3.Text);
      com.Parameters.Add("@ad", OleDbType.WChar).Value = prénom.Text;

      com.ExecuteNonQuery();
    }

    MessageBox.Show("OK");
  }
  catch (Exception ex)
  {
    MessageBox.Show(string.Format("Erreur. Message: '{0}'", ex.Message));
  }
  finally
  {
    con.Close();
  }
}

Bonne journée,

Billou_13
Bask En Force

--------------------------------------------------------------------
Connaître la réponse est une chose, savoir pourquoi en est une autre
---------------------
0
cs_fayrous Messages postés 48 Date d'inscription lundi 23 février 2009 Statut Membre Dernière intervention 30 avril 2009
20 mars 2009 à 16:02
merci beaucoup Billou_13 .
0
Rejoignez-nous