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
| OracleCommand MaCom;
DataSet MyDs = new DataSet();
OracleDataAdapter MonAdapter;
OracleConnection MaCon;
try
{
// chaine de connexion pour excel
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NomFic +
@";Extended Properties=""Excel 8.0;HDR=YES;""";
// création du provider pour excel
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
// création de l'adapter
DbDataAdapter adapter = factory.CreateDataAdapter();
// création de la commande de selection
DbCommand selectCommand = factory.CreateCommand();
selectCommand.CommandText = "SELECT Matricule, Nom, Prénom, [C unité appartenance] " +
"FROM [EFFECTIF$]";
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = excelConnectionString;
selectCommand.Connection = connection;
adapter.SelectCommand = selectCommand;
// remplissage de mon dataset
adapter.Fill(MyDs);
//création de mon adapteur Oracle
MonAdapter = new OracleDataAdapter();
// initialisation de la connexion
MaCon = new OracleConnection("Data Source=Source;User ID=ID;Password=PWD;");
// création de la commande de selection
MaCom = new OracleCommand("Select Matricule, Nom, Prenom, MPE from system.salarie", MaCon);
MonAdapter.SelectCommand = MaCom;
// création de la commande d'insert
MaCom = new OracleCommand("insert into system.salarie (Matricule, Nom, " +
"Prenom, MPE) values (?, ?, ?, ?)", MaCon);
// Ajout des paramètres
MaCom.Parameters.Add("Matricule", OracleDbType.Int32);
MaCom.Parameters.Add("Nom", OracleDbType.Varchar2);
MaCom.Parameters.Add("Prénom", OracleDbType.Varchar2);
MaCom.Parameters.Add("[C unité appartenance]", OracleDbType.Varchar2);
// Affectation de la commande MaCom à la propriété insertcommand de MonAdapter
MonAdapter.InsertCommand = MaCom;
// création de la commande d'update
MaCom = new OracleCommand("Update system.salarie set MPE = ? where Matricule = ?", MaCon);
MaCom.Parameters.Add("[C unité appartenance]", OracleDbType.Varchar2);
MaCom.Parameters.Add("Matricule", OracleDbType.Int32);
MonAdapter.UpdateCommand = MaCom;
// Création de la commande de suppression
MaCom = new OracleCommand("delete from system.salarie where Matricule = ?", MaCon);
MaCom.Parameters.Add("Matricule", OracleDbType.Int32);
MonAdapter.DeleteCommand = MaCom;
//Mise à jour de la base et affichage du nombre de ligne modifiées, la table [Effectif$] de mon dataset s'appelle Table, (mais je n'ai pas compris pourquoi)
MessageBox.Show(MonAdapter.Update(MyDs, "Table").ToString()); |
Partager