Mise a jour d'une base des données

hfozepofpozfpfezpz Messages postés 4 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 2 mai 2011 - 9 mai 2009 à 17:45
hfozepofpozfpfezpz Messages postés 4 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 2 mai 2011 - 11 mai 2009 à 20:37
Bonjour,

j'aimerai ajouter un enregistrement a une base
des données Sql server.Merci d'avance

Mehdi

3 réponses

cs_MoH008 Messages postés 238 Date d'inscription lundi 2 mars 2009 Statut Membre Dernière intervention 29 septembre 2011 1
9 mai 2009 à 19:38
Salut,
regarde vers les classes sqlconnection, sqlcommand...

The possible meet the impossible to become the possimpible !!
0
hfozepofpozfpfezpz Messages postés 4 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 2 mai 2011
11 mai 2009 à 20:26
Merci pour votre réponse,

en faite j'ai developpé cet partie du code:

private int operation = 0;
//numero de la ligne en cours
private int RowNbr;
//objet DataRow
private DataRow dataRow;
private SqlCommandBuilder commandBuilder;
private SqlCommandBuilder cb;


string ConnexionString = @"Data Source=(local);Initial Catalog=CADETCCI08;" + "Integrated Security=SSPI;";
//string ConnexionString = @"Data Source=(local);Initial Catalog=CCI_Store;" + "Integrated Security=SSPI;";

string RequeteMagasin = "Select * from Magasin";
string RequeteBanque = "SELECT * FROM Banque";

SqlConnection connection;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
DataTable table = new DataTable();
SqlCommand sq;
SqlDataReader dr;
DataColumn col;
DataRow row;

#region Modifier/Ajouter une activité
/// <summary>
/// Modifier/Ajouter une activité
/// </summary>
///
///
private void button7_Click(object sender, EventArgs e)
{

if (operation == 1)
{
connection = new SqlConnection(ConnexionString);
sq = connection.CreateCommand();
sq.CommandType = CommandType.Text;
sq.CommandText = "Select * from Activite ";

da.SelectCommand = sq;
DataTable tb = new DataTable();

//da.Fill(ds,"Activité"];
da.Fill(ds);
da.Fill(table);
table = ds.Tables["Activite"];

commandBuilder = new SqlCommandBuilder(da);
cb.GetInsertCommand();

// Moodifier une activité
dataRow = ds.Tables["Activite"].Rows[RowNbr];
dataRow["ACT_Code"] = this.comboBox1.Text;
dataRow["ACT_Libelle"] = this.comboBox2.Text;
dataRow["ACT_Etat"] = this.comboBox3.Text;
dataRow["ACT_Utilisateur"] = this.comboBox4.Text;
dataRow["ACT_DateCreationModif"] = this.comboBox5.Text;

commandBuilder = new SqlCommandBuilder(da);

button8.Enabled = true;


}

// Ajouter une activité
else if (operation == 2)
{
dataRow = ds.Tables["Activite"].NewRow();
dataRow["ACT_Code"] = this.comboBox1.Text;
dataRow["ACT_Libelle"] = this.comboBox2.Text;
dataRow["ACT_Etat"] = this.comboBox3.Text;
dataRow["ACT_Utilisateur"] = this.comboBox4.Text;
dataRow["ACT_DateCreationModif"] = this.comboBox5.Text;

ds.Tables["Activite"].Rows.Add(dataRow);
da.Update(ds,"Activite");

commandBuilder = new SqlCommandBuilder(da);

}

// Suppression d'une Activité
if (checkBox1.Checked == true)
{
string message = "Etes-vous sûr de vouloir supprimer cette activité?";
string caption = "Gestion des devises";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Affichage du MessageBox.
result = MessageBox.Show(message, caption, buttons);

if (result == System.Windows.Forms.DialogResult.Yes)
{
table.Rows[comboBox1.SelectedIndex].Delete();
table.Rows[comboBox2.SelectedIndex].Delete();

this.Close();
}
}

// Mise a jour de la table
ds.AcceptChanges();
da.Update(ds);

#region Ajout d une nouvelle activité
/// <summary>
/// Ajout d une nouvelle activité
/// </summary>
///
///
private void button8_Click(object sender, EventArgs e)
{
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
comboBox4.Text = "";
comboBox5.Text = "";
textBox1.Text = "";

button7.Enabled = true;
button8.Enabled = false;
if (button8.Enabled == false)
{
operation = 2;
}
else
{
operation = 1;
}
#end region

En exécutant ce code, il n ya pas des erreurs,
la géneration est reussie
mais aprés que j'ai ajouté une nouvelle enregistrement, et je valide, toujours pas
d'erreur, mais lorsque j'ai vérifié dans ma base des données, je ne trouve pas l'enregistrement que
j'ai ajouté.

Merci d'avance pour votres aides
0
hfozepofpozfpfezpz Messages postés 4 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 2 mai 2011
11 mai 2009 à 20:37
Merci pour votre réponse, en faite j'ai developpé cet partie du code:
private int operation = 0;

//numero de la ligne en cours
private int RowNbr;

//objet DataRow private DataRow dataRow;

private SqlCommandBuilder commandBuilder; private SqlCommandBuilder cb;

string ConnexionString = @"Data Source=(local);Initial Catalog=CADETCCI08;" + "Integrated Security=SSPI;";
//string ConnexionString = @"Data Source=(local);Initial Catalog=CCI_Store;" + "Integrated Security=SSPI;";

string RequeteMagasin "Select * from Magasin"; string RequeteBanque "SELECT * FROM Banque";

SqlConnection connection;
DataSet ds = new DataSet();
SqlDataAdapter da new SqlDataAdapter(); DataTable table new DataTable();

SqlCommand sq; SqlDataReader dr;
DataColumn col; DataRow row;

Modifier/Ajouter une activité

void button7_Click(object sender, EventArgs e)
{ if (operation == 1)
{ connection = new SqlConnection(ConnexionString);
sq = connection.CreateCommand();
sq.CommandType = CommandType.Text;
sq.CommandText "Select * from Activite "; da.SelectCommand sq;

DataTable tb = new DataTable();
da.Fill(ds,"Activité"];
da.Fill(ds);
da.Fill(table);

table = ds.Tables["Activite"];

commandBuilder = new SqlCommandBuilder(da); cb.GetInsertCommand();

Moodifier une activité
dataRow ds.Tables["Activite"].Rows[RowNbr]; dataRow["ACT_Code"] this.comboBox1.Text;
dataRow["ACT_Libelle"] = this.comboBox2.Text; dataRow["ACT_Etat"] = this.comboBox3.Text;

dataRow["ACT_Utilisateur"] =this.comboBox4.Text; dataRow["ACT_DateCreationModif"] = this.comboBox5.Text;

commandBuilder = new SqlCommandBuilder(da);

button8.Enabled = true; }

// Ajouter une activité

else if (operation == 2)

{ dataRow ds.Tables["Activite"].NewRow(); dataRow["ACT_Code"] this.comboBox1.Text;
dataRow["ACT_Libelle"] = this.comboBox2.Text; dataRow["ACT_Etat"] = this.comboBox3.Text;
dataRow["ACT_Utilisateur"] = this.comboBox4.Text; dataRow["ACT_DateCreationModif"] = this.comboBox5.Text; ds.Tables

["Activite"].Rows.Add(dataRow);
da.Update(ds,"Activite");

commandBuilder = new SqlCommandBuilder(da); }

// Suppression d'une Activité

if (checkBox1.Checked == true)

{
string message = "Etes-vous sûr de vouloir supprimer cette activité?";

string caption "Gestion des devises"; MessageBoxButtons buttons MessageBoxButtons.YesNo;

DialogResult result;

// Affichage du MessageBox. result = MessageBox.Show(message, caption, buttons);

if (result == System.Windows.Forms.DialogResult.Yes)

{ table.Rows[comboBox1.SelectedIndex].Delete(); table.Rows[comboBox2.SelectedIndex].Delete(); this.Close(); } } // Mise a jour de la table

ds.AcceptChanges(); da.Update(ds);

// Ajout d une nouvelle activité

private void button8_Click(object sender, EventArgs e)

{

comboBox1.Text ""; comboBox2.Text ""; comboBox3.Text = ""; comboBox4.Text = ""; comboBox5.Text = ""; textBox1.Text = "";

button7.Enabled = true;
button8.Enabled = false;

if (button8.Enabled false) { operation 2; } else { operation = 1; }

En exécutant ce code, il n ya pas des erreurs, la géneration est reussie mais aprés que j'ai ajouté une nouvelle enregistrement, et je valide, toujours pas d'erreur, mais lorsque j'ai vérifié dans ma base des données, je ne trouve pas l'enregistrement que j'ai ajouté. Merci d'avance pour votres aides
0
Rejoignez-nous