Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing a program send data to excel by using oledb. I used Update statement like next:
C#
OleDbConnection MyConnection = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + GeneralData.excelPath + "';Extended Properties=Excel 8.0;")
MyConnection.Open();
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = MyConnection;
myCommand.CommandType = System.Data.CommandType.Text;
 string sql = "Update [test$] set press = " + pointsProperties[i].Pressure + ", temp = " + pointsProperties[i].Temperature + " where id= " + id;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();

The problem is I will use the sql statement more than 100 times, which takes much time, so I thought that using Data Table will take less time, so I wrote a code saving my data in data table like next:
C#
public static System.Data.DataTable ExcelDataTable = new System.Data.DataTable("Steam Properties");

static System.Data.DataColumn columnID = new System.Data.DataColumn("ID", System.Type.GetType("System.Int32"));
static System.Data.DataColumn columnPress = new System.Data.DataColumn("Press", System.Type.GetType("System.Int32"));
static System.Data.DataColumn columnTemp = new System.Data.DataColumn("Temp", System.Type.GetType("System.Int32"));

public static void IntializeDataTable() // Called one time in MDIParent1.Load()
        {
            columnID.DefaultValue = 0;
            columnPress.DefaultValue = 0;
            columnTemp.DefaultValue = 0;

            ExcelDataTable.Columns.Add(columnID);
            ExcelDataTable.Columns.Add(columnPress);
            ExcelDataTable.Columns.Add(columnTemp);
        }

public static void setPointInDataTable(StreamProperties Point)
        {
            System.Data.DataRow ExcelDataRow = ExcelDataTable.NewRow(); // Must be decleared inside the function
                                                                        // It will raise exception if decleared outside the function
            ExcelDataRow["ID"] = Point.ID;
            ExcelDataRow["Press"] = Point.Pressure;
            ExcelDataRow["Temp"] = Point.Temperature;

            ExcelDataTable.Rows.Add(ExcelDataRow);
        }

The problem is I don’t know :

1- Is the second way is faster?

2- How to copy the Data Table to the excel file?

Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900