Se connecter à une base de données MS SQL Serveur à l’aide de connexion OledDB

Author:

Recherche,dans,la,base,de,données:,non,édité
{filelink=16148}

using System;
using System.Data.OleDb;

public class ConnexionBaseExcel
{
  [STAThread]
  static void Main(string[] args)
  {
    String chaineConnexion = @"provider=sqloledb;server=(local);database=MesExemples.com;Integrated Security=SSPI";

    String sSQL = "select * from Clients";

    OleDbConnection connexion = new OleDbConnection(chaineConnexion);
    connexion.Open();

    OleDbCommand oledComm = new OleDbCommand(sSQL, connexion);
    OleDbDataReader oledbReader = oledComm.ExecuteReader();

    int idxID = oledbReader.GetOrdinal("id");
    int idxSociété = oledbReader.GetOrdinal("Société");
    int idxAdresse = oledbReader.GetOrdinal("Adresse");

    while(oledbReader.Read()) {
      Console.WriteLine("{0} {1} {2}",
        oledbReader.GetValue(idxID),
        oledbReader.GetValue(idxSociété),
        oledbReader.GetValue(idxAdresse));
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *