【发布时间】:2016-07-04 09:15:05
【问题描述】:
我有一个 excel 文件,其中包含一些记录并且记录会在一秒钟内刷新。我想将所有记录存储在 SQL Server 数据库中。所以我的意图是每 500 毫秒后从这个文件中读取数据到我的数据库中。我已经成功地能够读取数据并存储在我的数据库中。但几分钟后,excel 挂起并处于无响应状态。
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)
{
}
}
【问题讨论】:
-
尝试释放或处置,就像在这个示例中:stackoverflow.com/questions/158706/…
-
我的建议有帮助吗?