insertion ou mise à jour C#
Bonjour,
j'ai une table sql dans la quelle je doit insérer des données ou mettre à jour si ces dernier existent.
voici les noms des colonnes de ma table : NOM,PRENOM,DAT_NAISS,FONCTION,SALAIRE.
le nom de la table est employee.
les 3 champs : Nom, Prenom, DatNaiss représentent la clé primaire.
L'insertion et la mise à jour ce fait de cette manière : Les données arrivent sous forme d'un fichier excel, je charge le fichier dans un Dataset, et puis à partir du dataSet à la Table Sql.
Au moment de l’insertion si un employée existe il sera mis à jour sinon il sera insérer.
mon problème ce 'est que je sais pas comment dire au programme à quel moment il doit faire une insertion ou une mise à jour.
voici un morceau de code de base (incomplet):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| class employee_Operations
{
Connexion connexion = new Connexion();
private SqlConnection connection = null;
public employee_Operations()
{
// Conenction to the database
//this.connection = connexion.open_onnection();
}
public void Enregistrer_donnees(DataSet monDataSet)
{
//try
//{
this.connection = connexion.open_onnection();
string qry = @"insert into employee(NOM,PRENOM,DAT_NAISS,FONCTION,SALAIRE)
values(@nom,@prenom,@datnaiss,@fonction,@salaire)";
SqlCommand mycommand = new SqlCommand(qry, this.connection);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry, connection);
SqlParameter param1 = new SqlParameter("@nom",qlDbType.VarChar);
SqlParameter param2 = new SqlParameter("@prenom",DbType.VarChar);
SqlParameter param3 = new SqlParameter("@datSqlDbType.SmallDateTime);
SqlParameter param4 = new SqlParameter("@fonSqlDbType.VarChar);
SqlParameter param5 = new SqlParameter("@salSqlDbType.Int);
mycommand.Parameters.Add(param1);
mycommand.Parameters.Add(param2);
mycommand.Parameters.Add(param3);
mycommand.Parameters.Add(param4);
mycommand.Parameters.Add(param5);
foreach (DataRow dr in monDataSet.Tables[0].Rows)
{
param1.Value = dr[0];
param2.Value = dr[1];
param3.Value = dr[2];
param4.Value = dr[3];
param5.Value = dr[4];
mycommand.ExecuteNonQuery();
}
this.connection.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
} |
et voici la calass connexion :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace GESTTIME
{
class Connexion
{
public string connexion = "Data Source=MARICHE\\SQLEXPRESS;Initial Catalog=carmonitor;integrated security = true;";
public Connexion()
{
//
// TODO: Add constructor logic here
//
}
public SqlConnection open_onnection()
{
SqlConnection con = new SqlConnection(connexion);
con.Open();
return con;
}
public void close_onnection()
{
SqlConnection con = new SqlConnection();
con.Close();
}
}
} |
Merci d'avance mes amis!!!