Jump to content

[C# + Access] Problème de syntaxe INSERT INTO

Featured Replies

Posted

Bonjour

J'ai un souci avec mon programme.

Je souhaite faire des mise à jours d'une base de données Access, mais quand je renseigne les TextBox, et que je valide ( aussi bien en Update, delete, et insert), j'ai une erreur de syntaxe qui arrive.

Voici mon code:

//********************************************************************************
*****************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
	OleDbConnection m_cnADONetConnection = new OleDbConnection();
	OleDbDataAdapter m_daDataAdapter;
	DataTable m_dtCruas = new DataTable();
	int rowPosition = 0;
	public Form1()
	{
		InitializeComponent();
	}

	//---------------------------------------------------------------------------------------
	private void Form1_Load(object sender, EventArgs e)
	{
		// TODO : cette ligne de code charge les données dans la table 'cruasDataSet.Cruas'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
		this.cruasTableAdapter.Fill(this.cruasDataSet.Cruas);
		m_cnADONetConnection.ConnectionString =
		   @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Users\Moi\Desktop\Cruas.mdb";
		m_cnADONetConnection.Open();
		m_daDataAdapter =
			new OleDbDataAdapter("Select * From Cruas", m_cnADONetConnection);
		OleDbCommandBuilder m_cbCommandBuilder =
			new OleDbCommandBuilder(m_daDataAdapter);
		m_daDataAdapter.Fill(m_dtCruas);

		this.ShowCurrentRecord();
	}

	//------------------------------------------------------------------------------------------

	private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
	{
		m_cnADONetConnection.Close();
		m_cnADONetConnection.Dispose();
	}

	//--------------------------------------------------------------------------------------

	private void ShowCurrentRecord()
	{
		if (m_dtCruas.Rows.Count == 1)
		{
			txtNewNumeroPI.Text = "";
			txtNewAdresseMAC.Text = "";
			txtNewSwitch.Text = "";
			txtNewLieu.Text = "";
			return;
		}

		txtNewNumeroPI.Text = m_dtCruas.Rows[rowPosition]["Numero PI"].ToString();
		txtNewAdresseMAC.Text = m_dtCruas.Rows[rowPosition]["Adresse MAC"].ToString();
		txtNewSwitch.Text = m_dtCruas.Rows[rowPosition]["Switch"].ToString();
		txtNewLieu.Text = m_dtCruas.Rows[rowPosition]["Lieu"].ToString();
	}

	//--------------------------------------------------------------------------------------
	/*private void Ajouter_Click(object sender, EventArgs e)
	{
		DataRow drNewRow = m_dtCruas.NewRow();
		drNewRow["Numero PI"] = "";						 //txtNewContactName.Text;
		drNewRow["Adresse MAC"] = "";					   //txtNewState.Text;
		drNewRow["Switch"] = "";							//txtNewState.Text;
		drNewRow["Lieu"] = "";							  //txtNewState.Text;
		m_dtCruas.Rows.Add(drNewRow);
		m_daDataAdapter.Update(m_dtCruas);
		m_rowPosition = m_dtCruas.Rows.Count - 1;
		this.ShowCurrentRecord();		   
	}*/
	//---------------------------------------------------------------------------------------

	private void Quitter_Fenetre_Click(object sender, EventArgs e)
	{
		this.Close();
	}


	//----------------------------------------------------------------------------------------
	private void btnAddNew_Click(object sender, EventArgs e)
	{
		DataRow drNewRow = m_dtCruas.NewRow();
		drNewRow["Numero PI"] = txtNewNumeroPI.Text;
		drNewRow["Adresse MAC"] = txtNewAdresseMAC.Text;
		drNewRow["Switch"] = txtNewSwitch.Text;
		drNewRow["Lieu"] = txtNewLieu.Text;
		m_dtCruas.Rows.Add(drNewRow);

		/*string rqInsertCruas = "INSERT INTO Cruas(Numero PI, Adresse MAC, Switch, Lieu) "
		+ "VALUES (" txtNewNumeroPI.Text "; " + txtNewAdresseMAC.Text + "; " + txtNewSwitch.Text + "; " txtNewLieu.Text ");
		*/

		m_daDataAdapter.Update(m_dtCruas);
		rowPosition = m_dtCruas.Rows.Count - 1;
		this.ShowCurrentRecord();
	}

	//------------------------------------------------------------------------------------------

	private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
	{

	}


	//-------------------------------------------------------------------------------------------
	private void txtNewNumeroPI_TextChanged(object sender, EventArgs e)
	{

	}
	//-------------------------------------------------------------------------------------------
	private void Delete_Click(object sender, EventArgs e)
	{
		 // If there is data, delete the current row.
		if (m_dtCruas.Rows.Count != 0)
		{
			m_dtCruas.Rows[rowPosition].Delete();
			m_daDataAdapter.Update(m_dtCruas);
			rowPosition = 0;
			this.ShowCurrentRecord();
		}
	}

	private void button1_Click(object sender, EventArgs e)
	{
		// If there is existing data, update it.
		if (m_dtCruas.Rows.Count != 0)
		{
			m_dtCruas.Rows[rowPosition]["Numero PI"] = txtNewNumeroPI.Text;
			m_dtCruas.Rows[rowPosition]["Adresse MAC"] = txtNewAdresseMAC.Text;
			m_dtCruas.Rows[rowPosition]["Switch"] = txtNewSwitch.Text;
			m_dtCruas.Rows[rowPosition]["Lieu"] = txtNewLieu.Text;
			m_daDataAdapter.Update(m_dtCruas);
		}
	}

   //---------------------------------------------------------------------------------------------
}
}
//********************************************************************************
***

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

Je penserais que ton problème vient de tes noms de colonnes avec des espaces dans ta requete d'ajout/modif. Essaye en les entourant de crochets (propre a Access), ex :

drNewRow["Numero PI"] ==> drNewRow["[Numero PI]"]

Archived

This topic is now archived and is closed to further replies.