Read Excel -- cont.

Hello All,

I'm trying to make a class, that does all the jobs i want to do on excel.
Connect, Read, Update and Write.

for this part well just be discussing Reading from excel

I'm writing this in C#.

This is a work in progress. I've made a few other posts detailing to scope of this project.I've included these posts below. I'm a novice so bear with me.

For reference see:

http://www.cplusplus.com/forum/windows/2376/
http://www.cplusplus.com/forum/windows/2133/

Now this is just a rough draft... I just wanted to put it out there and see if everything looks alright, to the experts. I'll be working with it much more.

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
using System;
using System.Data;

public class excel
{
    private System.Data.OleDb.OleDbFactory DbProviderFactory;

	public void ExcelRead()
	{
        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Update.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");


    using (DbConnection connection = factory.CreateConnection())
    {
        connection.ConnectionString = connectionString;


    using (DbCommand command = connection.CreateCommand())
    {
        command.CommandText = "SELECT Price,Product FROM [PriceList$]"; // PriceList$ comes from the name of the worksheet
        connection.Open();

        using (DbDataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                Debug.WriteLine(dr["Product"].ToString());
            }
        }
    }
}
	}
}


Last edited on
Topic archived. No new replies allowed.