Reader Level:
ARTICLE

Connect to an Excel file as a data source via OBDC

Posted by Bechir Bejaoui Articles | ADO.NET June 30, 2008
This article provides answer to questions asked about how to connect to an excel file as a data source for a given application, I provide the solution via this ten steps walkthrough.
  • 0
  • 0
  • 32502

I wrote this article to answer a question asked about how to connect to an excel file as a data source for a given application, I provide the solution via this ten steps walkthrough:

Walkthrough:

Let's begin by creating an Excel file and configuring an ODBC connection

  1. Open an Excel file and populate some of its cells then save it somewhere
  2. Go to Start>Configuration panel>Administration tools> ODBC icon
  3. Open it  then select the tag data source system



    Figure 1

  4. Click add, then the following window Appears



    Figure 2

  5. Select the Driver do Microsoft Excel(*.xls)
  6. Then Apply and the bellow window appears



    Figure 3

  7. In the first field, define the Name or the alias going to be used, say we name it EXCEL for example, in the second field you can put a note according to this alias then click the button select a file, it helps you to browse to your given excel file then click ok, now the ODBC connection is ready to be used, this is the window after filling settings:



    Figure 4

  8. Now, as our connection is ready to be used let's create a new windows application project and add a data grid view, a button and a label  into the form that will appear like the bellow form:



    Figure 5

    Change the name of the button to btnconnect and change its text to "Connect to Excel data source", then change the label text to "".
  9. Add this code to the button click event handler.

    private void btnConnect_Click(object sender, EventArgs e)

            {

                OdbcConnection oConn = new OdbcConnection();

                oConn.ConnectionString = "Dsn=EXCEL";

                OdbcCommand oComm = new OdbcCommand();

                oComm.Connection = oConn;

                oComm.CommandText = "Select * From [Feuil1$A1:C3]";

                try

                {

                    DataSet ds = new DataSet();

                    OdbcDataAdapter oAdapter = new OdbcDataAdapter(oComm);

                    oConn.Open();

                    oAdapter.Fill(ds);

                    dataGridView1.DataSource = ds;

                    dataGridView1.DataMember = ds.Tables[0].TableName;

                    label1.Text = "Connection established successfully!!!";

                }

                catch (IOException caught) { MessageBox.Show(caught.Message); }

                catch (OdbcException caught) { MessageBox.Show(caught.Message); }

                finally

                {

                    oConn.Close();

                }

            }

    But don't forget to include the System.Data.Odbc name space into your project.
  10. Now, run the application and observe.


     
    Figure 6

Good dotneting!!!

COMMENT USING

Trending up