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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// J'ajoute une row a mon datatable et cela plusieurs oir si il y a plusieurs objets
DataRow ElementRow = Elem.NewRow();
ElementRow["Dgauche"] = Element.Distgauche;
ElementRow["Dgout"] = Element.Distgout;
ElementRow["Lar"] = Element.Lon;
ElementRow["Lon"] = Element.Lar;
ElementRow["Zone"] = Element.Zone;
ElementRow["Nom"] = Element.Nom;
ElementRow["Idproj"] = id_projet;
Elem.Rows.Add(ElementRow);
// Je boucle les rows pour creer des tableaux de string que j'envois dans la ethode de ma classe Base_donnees
foreach(DataRow row in Elem.Rows)
{
reussi = false;
newelem = new string[7];
newelem[0] = row["Dgauche"].ToString();
newelem[1] = row["Dgout"].ToString();
newelem[2] = row["Lar"].ToString();
newelem[3] = row["Lon"].ToString();
newelem[4] = row["Zone"].ToString();
newelem[5] = row["Nom"].ToString();
newelem[6] = id_projet.ToString();
IdEtat = Base_Donnees.Ajouter_Un_Enreg_A_Une_Table("Elem", newelem);
//Une ois dans la classe ca done ca
public Dictionary<int, bool> Ajouter_Un_Enreg_A_Une_Table(DataTable table, string[] Enreg)
{
/* Pour chaque champs de l'enregistrement en question
* On va créer la chaine nécessaire à l'ajoût d'un enregistrement */
string proprietes="";
for(int i=0;i<Enreg.Length;i++)
{
proprietes += (Enreg[i] + ",");
}
//on enlève la virgule de fin de chaîne
proprietes=proprietes.Substring(0,proprietes.Length-2);
//on vérifie que l'ajoût c'est bien passé
return (this.SQL.Ajouter_Un_Enreg(table,proprietes));
}
public Dictionary<int, bool> Ajouter_Un_Enreg(DataTable Table, string proprietes)
{
bool Etat=false;
OleDbConnection connec=new OleDbConnection(this.ParametresConnection);
Dictionary<int, bool> IdEtat = new Dictionary<int, bool>();
string champs="";
//on crée une string qui va prendre tous les noms des champs
//sauf l'ID
for(int i=1;i<Table.Columns.Count;i++)
champs+=Table.Columns[i].ColumnName + ",";
//pour enlever la virgule de fin de chaine
champs=champs.Substring(0,champs.Length-1);
try
{
string requete = "INSERT INTO " + Table + "(" + champs + ") VALUES(\'" + proprietes + "\');";
// requete = "INSERT INTO Elem(Dgauche,Dgout,Lar,Lon,Zone,Nom,Idproj) VALUES('100','100','100','100','100','100','26');";
//requete = "INSERT INTO usager(_user,_pwd) VALUES('100','100');";
OleDbCommand command=new OleDbCommand(requete,connec);
connec.Open();
Etat=(command.ExecuteNonQuery()>0);
OleDbCommand command2 = new OleDbCommand("SELECT @@IDENTITY", connec);
int Id = Convert.ToInt32(command2.ExecuteScalar());
IdEtat[Id] = Etat;
connec.Close();
}
catch
{
Etat=false;
}
return IdEtat;
} |
Partager