I am writing a program send data to excel by using oledb. I used Update statement like next:
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:
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.