Overblog
Suivre ce blog Administration + Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles RÉCents

26 mai 2007 6 26 /05 /mai /2007 10:20
Se connecter à un classeur Excel 2007 (*.xlsx) via ADO.NET
La chaine de connexion a la forme :
-          Provider=Microsoft.ACE.OLEDB.12.0;
-          Data Source=chemin complet vers le fichier Excel
-          Extended Properties=""Excel 12.0;HDR=YES;"""
private void button2_Click(object sender, EventArgs e)
        {
            System.Data.DataTable oDataTable = new DataTable();
            System.Data.OleDb.OleDbDataAdapter oOleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
            oOleDbDataAdapter.SelectCommand = new System.Data.OleDb.OleDbCommand();
            oOleDbDataAdapter.SelectCommand.Connection = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:Documents and SettingsromagnyMes documentsDIVERS.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""");
            oOleDbDataAdapter.SelectCommand.CommandType = CommandType.Text;
            oOleDbDataAdapter.SelectCommand.CommandText = "SELECT [ContactID],[ContactName],[ContactFirstName],[ContactEmail],[ContactCategoryID] FROM [Contact$]";
 
            oOleDbDataAdapter.Fill(oDataTable);
 
            dataGridView1.DataSource = oDataTable;
        }

http://www.connectionstrings.com/default.aspx?carrier=excel2007
Partager cet article
Repost0
4 février 2007 7 04 /02 /février /2007 19:42
Interaction Office [Excel] – ADO.NET
1 – La chaine de connexion :
sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:Documents and SettingsromagnyMes documentsContacts.xls"";Extended Properties=""Excel 8.0;HDR=YES;""";
(même si j'utilise ici Excel 2007, le format du classeur que j'utilise est un format Excel 2003,la chaine de connexion ci-dessus correspond  pour un fichier Excel 2003)
pour Excel 2007 se referrer à http://www.connectionstrings.com
2 – La requête
On peut appliquer une requête SELECTINSERT ou UPDATE

Par contre DELETE ne semble pas pris en charge .

                    SELECT [NOM],[PRENOM] FROM [CONTACT$]
>  NOM et PRENOM sont les noms des colonnes du classeur Excel 
  - > on peut également saisir * à la place du nom des colonnes bien entendu
CONTACT est le nom de la feuille visée Excel (que l’on peut renommer) (Excel crée 3 feuilles par classeur par défaut)
 -> On peut sélectionner plusieurs feuilles en même temps
ne pas oublier "$" à la fin de la saisie des noms de feuilles de classeur
exemple  >> SELECT * FROM [FEUIL1$],[FEUIL2$]

 
3 – méthodes pour charger un classeur Excel :
 
Avec DbProviderFactory
// METHODES
        public void ChargerDbProviderFactory()
        {
            System.Data.Common.DbProviderFactory oDbProviderFactory;
            System.Data.Common.DbConnection oDbConnection;
            string sConnectionString;
            System.Data.Common.DbCommand oDbCommand;
            System.Data.Common.DbDataReader oDbDataReader;
            System.Data.DataTable oDataTable;
 
            oDataTable = new DataTable();
 
            try
            {
                // DBPROVIDERFACTORY
                oDbProviderFactory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.OleDb");
                // CONNEXION
                sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:Documents and SettingsromagnyMes documentsContacts.xls"";Extended Properties=""Excel 8.0;HDR=YES;""";
                oDbConnection = oDbProviderFactory.CreateConnection();
                oDbConnection.ConnectionString = sConnectionString;
                // COMMAND
               oDbCommand = oDbConnection.CreateCommand();
                oDbCommand.CommandType = System.Data.CommandType.Text;
                oDbCommand.CommandText = "SELECT [NOM],[PRENOM] FROM [CONTACT$]";
 
                // EXECUTION
                oDbConnection.Open();
                oDbDataReader = oDbCommand.ExecuteReader();
                // !! Méthode LOAD de DATATABLE uniquement disponible en .NET 2.0
                oDataTable.Load(oDbDataReader);
                oDbDataReader.Close();
                oDbConnection.Close();
 
                // AFFICHAGE
                dataGridView1.DataSource = oDataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
       
 
Charger un DataReader
        public void ChargerDataReaderOleDb()
        {
            System.Data.OleDb.OleDbConnection oOleDbConnection;
            System.Data.OleDb.OleDbCommand oOleDbCommand;
            System.Data.OleDb.OleDbDataReader oOleDbDataReader;
            System.Data.DataTable oDataTable;
            string sConnectionString;
 
            try
            {
                oOleDbConnection = new System.Data.OleDb.OleDbConnection();
                oOleDbCommand = new System.Data.OleDb.OleDbCommand();
                oDataTable = new DataTable();
 
                // CONNEXION
                sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Documents and SettingsromagnyMes documentsContacts.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
                oOleDbConnection.ConnectionString = sConnectionString;
                // COMMAND
                oOleDbCommand.Connection = oOleDbConnection;
                oOleDbCommand.CommandType = System.Data.CommandType.Text;
                oOleDbCommand.CommandText = "SELECT [NOM],[PRENOM] FROM [CONTACT$]";
 
                // EXECUTION
                oOleDbConnection.Open();
                oOleDbDataReader = oOleDbCommand.ExecuteReader();
                // !! Méthode LOAD de DATATABLE uniquement disponible en .NET 2.0
                oDataTable.Load(oOleDbDataReader);
                oOleDbDataReader.Close();
                oOleDbConnection.Close();
 
                // AFFICHAGE
                dataGridView1.DataSource = oDataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
 
Charger une DataTable
        public void ChargerDataTableOleDb()
        {
            System.Data.OleDb.OleDbDataAdapter oOleDbDataAdapter;
            System.Data.DataTable oDataTable;
            string sConnectionString;
 
            try
            {
                oOleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
                oOleDbDataAdapter.SelectCommand=new System.Data.OleDb.OleDbCommand();
                oOleDbDataAdapter.SelectCommand.Connection = new System.Data.OleDb.OleDbConnection();
                oDataTable = new DataTable();
               
                // SELECTCOMMAND
                sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:Documents and SettingsromagnyMes documentsContacts.xls"";Extended Properties=""Excel 8.0;HDR=YES;""";
                oOleDbDataAdapter.SelectCommand.Connection.ConnectionString = sConnectionString;
                oOleDbDataAdapter.SelectCommand.CommandType=System.Data.CommandType.Text;
                oOleDbDataAdapter.SelectCommand.CommandText="SELECT [NOM],[PRENOM] FROM [CONTACT$]";
 
                //EXECUTION
                oOleDbDataAdapter.SelectCommand.Connection.Open();
                oOleDbDataAdapter.Fill(oDataTable);
                oOleDbDataAdapter.SelectCommand.Close();
             
                // AFFICHAGE
                dataGridView1.DataSource = oDataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

 

 

 

 

Partager cet article
Repost0
1 février 2007 4 01 /02 /février /2007 22:23

DataTable <---> DataReader

 

1 - Charger une DataTable avec un DataReader

            Persistance.OleDb.CONTACT oContact;
            oContact = new Persistance.OleDb.CONTACT();
            Persistance.OleDbConnecte.sCONNECTIONSTRING = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Documents and SettingsromagnyMes documentsContacts.mdb";
            System.Data.DataTable oDataTable;
            oDataTable = new DataTable();
 
oDataTable.Load(oContact.ChargerCONTACTs()); //oContact.ChargerCONTACTs() est une méthode retournant un datareader
             // Affichage
            comboBox1.DataSource = oDataTable;
            comboBox1.ValueMember = "nom_contact";

 
2 - Créer un DataReader(avec System.Data.Common.DbDataReader)à partir d’une DataTable

            System.Data.Common.DbDataReader oDbDataReader;
            oDbDataReader = oDataTable.CreateDataReader();
            while (oDbDataReader.Read())
            {
                comboBox2.Items.Add(oDbDataReader["nom_contact"]);
            }
            oDbDataReader.Close();

 

 

Partager cet article
Repost0