[C# + Access] Ajout, suppression et modifications des données

Ajout, suppression et modifications des données [C# + Access] - C#/.NET managed - Programmation

Marsh Posté le 07-03-2009 à 18:46:46    

Bonjour
 
J'ai un souci avec mon programme.
 
Je souhaite faire des mise à jours d'une base de données Access, maisquand je renseigne les TextBox, et que je valide ( aussi bien enUpdate, delete, et insert), j'ai une erreur de syntaxe qui arrive.
 
Voici mon code:

Code :
  1. //*************************************************************************************************
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.OleDb;
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         OleDbConnection m_cnADONetConnection = new OleDbConnection();
  15.         OleDbDataAdapter m_daDataAdapter;
  16.         DataTable m_dtCruas = new DataTable();
  17.         int rowPosition = 0;
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.         //---------------------------------------------------------------------------------------
  23.         private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.             // TODO : cette ligne de code charge les données dans latable 'cruasDataSet.Cruas'. Vous pouvez la déplacer ou la supprimerselon vos besoins.
  26.             this.cruasTableAdapter.Fill(this.cruasDataSet.Cruas);
  27.             m_cnADONetConnection.ConnectionString =
  28.                @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:UsersMoiDesktopCruas.mdb";
  29.             m_cnADONetConnection.Open();
  30.             m_daDataAdapter =
  31.                 new OleDbDataAdapter("Select * From Cruas", m_cnADONetConnection);
  32.             OleDbCommandBuilder m_cbCommandBuilder =
  33.                 new OleDbCommandBuilder(m_daDataAdapter);
  34.             m_daDataAdapter.Fill(m_dtCruas);
  35.             this.ShowCurrentRecord();  
  36.         }
  37.         //------------------------------------------------------------------------------------------
  38.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  39.         {
  40.             m_cnADONetConnection.Close();
  41.             m_cnADONetConnection.Dispose();
  42.         }
  43.         //--------------------------------------------------------------------------------------
  44.         private void ShowCurrentRecord()
  45.         {
  46.             if (m_dtCruas.Rows.Count == 1)
  47.             {
  48.                 txtNewNumeroPI.Text = "";
  49.                 txtNewAdresseMAC.Text = "";
  50.                 txtNewSwitch.Text = "";
  51.                 txtNewLieu.Text = "";
  52.                 return;
  53.             }
  54.             txtNewNumeroPI.Text = m_dtCruas.Rows[rowPosition]["Numero PI"].ToString();
  55.             txtNewAdresseMAC.Text = m_dtCruas.Rows[rowPosition]["Adresse MAC"].ToString();
  56.             txtNewSwitch.Text = m_dtCruas.Rows[rowPosition]["Switch"].ToString();
  57.             txtNewLieu.Text = m_dtCruas.Rows[rowPosition]["Lieu"].ToString();
  58.         }
  59.         //--------------------------------------------------------------------------------------
  60.         /*private void Ajouter_Click(object sender, EventArgs e)
  61.         {
  62.             DataRow drNewRow = m_dtCruas.NewRow();
  63.             drNewRow["Numero PI"] = "";                         //txtNewContactName.Text;
  64.             drNewRow["Adresse MAC"] = "";                       //txtNewState.Text;
  65.             drNewRow["Switch"] = "";                            //txtNewState.Text;
  66.             drNewRow["Lieu"] = "";                              //txtNewState.Text;
  67.             m_dtCruas.Rows.Add(drNewRow);
  68.             m_daDataAdapter.Update(m_dtCruas);
  69.             m_rowPosition = m_dtCruas.Rows.Count - 1;
  70.             this.ShowCurrentRecord();             
  71.         }*/
  72.         //---------------------------------------------------------------------------------------
  73.         private void Quitter_Fenetre_Click(object sender, EventArgs e)
  74.         {
  75.             this.Close();
  76.         }
  77.         //----------------------------------------------------------------------------------------
  78.         private void btnAddNew_Click(object sender, EventArgs e)
  79.         {
  80.             DataRow drNewRow = m_dtCruas.NewRow();
  81.             drNewRow["Numero PI"] = txtNewNumeroPI.Text;
  82.             drNewRow["Adresse MAC"] = txtNewAdresseMAC.Text;
  83.             drNewRow["Switch"] = txtNewSwitch.Text;
  84.             drNewRow["Lieu"] = txtNewLieu.Text;
  85.             m_dtCruas.Rows.Add(drNewRow);
  86.             /*string rqInsertCruas = "INSERT INTO Cruas(Numero PI, Adresse MAC, Switch, Lieu) "
  87.             + "VALUES (" txtNewNumeroPI.Text "; " + txtNewAdresseMAC.Text + "; " + txtNewSwitch.Text + "; " txtNewLieu.Text " );
  88.             */
  89.            
  90.             m_daDataAdapter.Update(m_dtCruas);
  91.             rowPosition = m_dtCruas.Rows.Count - 1;
  92.             this.ShowCurrentRecord();
  93.         }
  94.         //------------------------------------------------------------------------------------------
  95.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  96.         {
  97.            
  98.         }
  99.         //-------------------------------------------------------------------------------------------
  100.         private void txtNewNumeroPI_TextChanged(object sender, EventArgs e)
  101.         {
  102.         }
  103.         //-------------------------------------------------------------------------------------------
  104.         private void Delete_Click(object sender, EventArgs e)
  105.         {
  106.              // If there is data, delete the current row.
  107.             if (m_dtCruas.Rows.Count != 0)
  108.             {
  109.                 m_dtCruas.Rows[rowPosition].Delete();
  110.                 m_daDataAdapter.Update(m_dtCruas);
  111.                 rowPosition = 0;
  112.                 this.ShowCurrentRecord();
  113.             }
  114.         }
  115.         private void button1_Click(object sender, EventArgs e)
  116.         {
  117.             // If there is existing data, update it.
  118.             if (m_dtCruas.Rows.Count != 0)
  119.             {
  120.                 m_dtCruas.Rows[rowPosition]["Numero PI"] = txtNewNumeroPI.Text;
  121.                 m_dtCruas.Rows[rowPosition]["Adresse MAC"] = txtNewAdresseMAC.Text;
  122.                 m_dtCruas.Rows[rowPosition]["Switch"] = txtNewSwitch.Text;
  123.                 m_dtCruas.Rows[rowPosition]["Lieu"] = txtNewLieu.Text;
  124.                 m_daDataAdapter.Update(m_dtCruas);
  125.             }
  126.         }
  127.        //---------------------------------------------------------------------------------------------
  128.     }
  129. }
  130. //***********************************************************************************


Je pense que je m'y prends mal non ?
 
Autre question:  
 
- Est ce que après avoir effectuer la moindre modification, je peut voir sur la même fenêtre via le DataGridView affichant le contenu de ma base de données ?  ( une base toute simple d'une seule table)
 
Merci de votre aide

Reply

Marsh Posté le 07-03-2009 à 18:46:46   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed