Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
bases_programmation:acces_aux_bases_de_donnees_sgbdr [2017/08/08 07:00]
admin
bases_programmation:acces_aux_bases_de_donnees_sgbdr [2017/08/08 08:39] (Version actuelle)
admin [Connexion ODBC]
Ligne 57: Ligne 57:
 command.Parameters.AddWithValue("​@id",​ 3); command.Parameters.AddWithValue("​@id",​ 3);
 </​code>​ </​code>​
 +
 +
 +===== Lire les résultats d'une requête =====
 +<​code>​
 +// Lecture des résultats
 +SqlDataReader dataReader = command.ExecuteReader();​
 +
 +while (dataReader.Read())
 +{
 +     ​comboBox.Items.Add(dataReader["​nom"​]);​
 +}
 +</​code>​
 +
 +
 +
 +===== Ajouter, modifier, supprimer des données =====
 +<​code>​
 +// Définition de la requête
 +command.CommandText = "​INSERT INTO couleur (id, nom) VALUES (@id, @nom)";​
 +command.Parameters.AddWithValue("​@id",​ 1);
 +command.Parameters.AddWithValue("​@nom",​ "​Violet"​);​
 +
 +// Exécution de la requête
 +Command.ExecuteNonQuery();​
 +</​code>​
 +<WRAP center round tip 60%>
 +Le principe reste le même pour des requêtes UPDATE ou DELETE.
 +</​WRAP>​
 +
 +===== Connexion ODBC =====
 +
 +<​code>​
 +using System.Data; ​   ​
 +using Microsoft.Data.Odbc;​
 +
 +string myConnectionString = ""​Driver={SQL Server};​Server=mySQLServer;​UID=***;​PWD=***;​Database=***;"";​
 +string mySelectQuery = "​Select * from Client";​
 +
 +public void CreateMyOdbcDataReader(string mySelectQuery,​string myConnectionString) ​
 +{
 +   ​OdbcConnection myConnection = new OdbcConnection(myConnectionString);​
 +   ​OdbcCommand myCommand = new OdbcCommand(mySelectQuery,​ myConnection);​
 +   ​myConnection.Open();​
 +   ​OdbcDataReader myReader = myCommand.ExecuteReader();​
 +   try
 +   {
 +     ​while(myReader.Read()) ​
 +     {
 +        Console.WriteLine(myReader.GetString(0));​
 +     }
 +   }
 +   ​finally
 +   {
 +     ​myReader.Close();​
 +     ​myConnection.Close();​
 +   }
 +}
 +       }
 +</​code>​
 +<WRAP center round tip 60%>
 +Il faut télécharger le fournisseur managé ODBC .NET sur le site Web de Microsoft à l’adresse suivante :
 +http://​www.microsoft.com/​downloads/​details.aspx?​familyid=6ccd8427-1017-4f33-a062-d165078e32b1
 +</​WRAP>​
 +