I have an excel file that contains some records and that records that refresh within a second. I want to store all records in an SQL Server Database. So My intention is to read data from this file into my database after every 500 milliseconds. I have successfully been able to read the data and store in my database. But after few minutes, excel hangs and is in a not responding state.
Excel._Application excel = (Excel._Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
clsDBOLiveData objDBOLiveData = new clsDBOLiveData();
clsDatabase objDatabase = new clsDatabase();
private static System.Timers.Timer aTimerLive_1;
private static System.Timers.Timer aTimerHist_1;
private static System.Timers.Timer aTimerLive_2;
private static System.Timers.Timer aTimerHist_2;
//Here I declare all variables.
public TradeData()
{
SetTimer1_Data();
SetTimer2_Data();
SetTimerHist_1_Data();
SetTimerHist_2_Data();
}
private void SetTimerLiveMCX_1_Data()
{
aTimerLive_1 = new System.Timers.Timer(500);
aTimerLive_1.Elapsed += OnTimedEvent_1;
aTimerLive_1.AutoReset = true;
aTimerLive_1.Enabled = true;
}
private void OnTimedEvent_1(Object source, ElapsedEventArgs e)
{
try
{
Excel.Workbook workbook = excel.Workbooks[1];
Excel.Worksheet workSheet = workbook.Worksheets.get_Item(1);
Excel.Range range = workSheet.UsedRange;
for (int j = 3; j < range.Rows.Count; j = j + 5)
{
if ((range.Cells[j, 2] as Excel.Range).Value2 == null || Convert.ToString((range.Cells[j, 2] as Excel.Range).Value2)=="")
continue;
clsDBOLiveData objDBOLiveData = new clsDBOLiveData();
objDBOLiveData.SYMBOL_NAME = (string)(range.Cells[j, 8] as Excel.Range).Value2;
objDBOLiveData.BAR_TIME= (double)(range.Cells[j, 2] as Excel.Range).Value2;
objDBOLiveData.HIGH= (decimal)(range.Cells[j, 3] as Excel.Range).Value2;
objDBOLiveData.LAST = (decimal)(range.Cells[j, 4] as Excel.Range).Value2;
objDBOLiveData.LOW = (decimal)(range.Cells[j, 5] as Excel.Range).Value2;
objDBOLiveData.OPEN = (decimal)(range.Cells[j, 6] as Excel.Range).Value2;
objDBOLiveData.VOLUME = (decimal)(range.Cells[j, 7] as Excel.Range).Value2;
objDBOLiveData.STATUS = (string)(range.Cells[j, 9] as Excel.Range).Value2;
string strErrorMgs = "";
if (!objDatabase.SaveData_1(objDBOLiveData, ref strErrorMgs)){}
}
}
catch(Exception ex)
{
}
}