G
Guest
I need to log 400 values every second into an access database. Previously I
did this in C++ using OLEDB and after a lot of experimentation, I minimized
the peak CPU usage by writing out the data in a separate thread every 10ms.
Now I have to do this in C# and I am uncertain as to the most efficient
method (I am primarily concerned with CPU usage).
My current methodology is below. One inefficiency I recognize but I’m not
sure how to fix is that I’m storing the data in memory as well as the
database but I only really need it in the database. Another specific question
I have is which database adaptor is fastest with access and which update
override is fastest (maybe rows is better than tables?)
Thanks,
Jeff
{
string strSQL;
OleDbCommandBuilder cmdBuilder;
DataSet dsIOChannelData;
m_dbConnection = new OleDbConnection(strConnection);
strSQL = "SELECT TimeStamp, ChannelID, Value FROM tblIOChannelData";
daIOChannelData = new OleDbDataAdapter(strSQL, m_dbConnection);
cmdBuilder = new OleDbCommandBuilder(daIOChannelData);
cmdBuilder.QuotePrefix = "[";
cmdBuilder.QuoteSuffix = "]";
cmdBuilder.SetAllValues = true;
dsIOChannelData = new DataSet();
m_dbConnection.Open();
daIOChannelData.Fill(dsIOChannelData, "tblIOChannelData");
dtIOChannelData = dsIOChannelData.Tables[0];
}
public void LogIO()
{
long lStartTicks;
long lElapsedTicks;
double dInverseTicksPerMS;
OleDbDataAdapter dbAdaptor;
DataTable dtIOChannelData;
loadIOLogDataTable(out dbAdaptor, out dtIOChannelData);
dInverseTicksPerMS = 1.0 / TimeSpan.TicksPerMillisecond;
m_bActiveLog = true;
while (m_bActiveLog) {
lStartTicks = DateTime.Now.Ticks;
for (int i = 0; i != 400; ++i)
dtIOChannelData.Rows.Add(DateTime.Now.ToOADate(), 77.5, 5);
dbAdaptor.Update(dtIOChannelData);
lElapsedTicks = DateTime.Now.Ticks - lStartTicks;
Thread.Sleep(Math.Max(0, 1000 - (int)(lElapsedTicks *
dInverseTicksPerMS + 0.5)));
}
m_dbConnection.Close();
}
did this in C++ using OLEDB and after a lot of experimentation, I minimized
the peak CPU usage by writing out the data in a separate thread every 10ms.
Now I have to do this in C# and I am uncertain as to the most efficient
method (I am primarily concerned with CPU usage).
My current methodology is below. One inefficiency I recognize but I’m not
sure how to fix is that I’m storing the data in memory as well as the
database but I only really need it in the database. Another specific question
I have is which database adaptor is fastest with access and which update
override is fastest (maybe rows is better than tables?)
Thanks,
Jeff
{
string strSQL;
OleDbCommandBuilder cmdBuilder;
DataSet dsIOChannelData;
m_dbConnection = new OleDbConnection(strConnection);
strSQL = "SELECT TimeStamp, ChannelID, Value FROM tblIOChannelData";
daIOChannelData = new OleDbDataAdapter(strSQL, m_dbConnection);
cmdBuilder = new OleDbCommandBuilder(daIOChannelData);
cmdBuilder.QuotePrefix = "[";
cmdBuilder.QuoteSuffix = "]";
cmdBuilder.SetAllValues = true;
dsIOChannelData = new DataSet();
m_dbConnection.Open();
daIOChannelData.Fill(dsIOChannelData, "tblIOChannelData");
dtIOChannelData = dsIOChannelData.Tables[0];
}
public void LogIO()
{
long lStartTicks;
long lElapsedTicks;
double dInverseTicksPerMS;
OleDbDataAdapter dbAdaptor;
DataTable dtIOChannelData;
loadIOLogDataTable(out dbAdaptor, out dtIOChannelData);
dInverseTicksPerMS = 1.0 / TimeSpan.TicksPerMillisecond;
m_bActiveLog = true;
while (m_bActiveLog) {
lStartTicks = DateTime.Now.Ticks;
for (int i = 0; i != 400; ++i)
dtIOChannelData.Rows.Add(DateTime.Now.ToOADate(), 77.5, 5);
dbAdaptor.Update(dtIOChannelData);
lElapsedTicks = DateTime.Now.Ticks - lStartTicks;
Thread.Sleep(Math.Max(0, 1000 - (int)(lElapsedTicks *
dInverseTicksPerMS + 0.5)));
}
m_dbConnection.Close();
}