C# DataSet 90 seconds faster than MFC CRecordset ?!?!?!?!?!?!?!?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
ok Im just trying to execute a query into a recordset, nothing out of the ordinary here.<br/>
C# Code:<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; while (reader.Read())
{
<span style="color:Blue; int i1 = reader.GetInt32(0),
i2 = reader.GetInt16(1),
i3 = 0;

<span style="color:Blue; if (!reader.IsDBNull(2))
{
i3 = reader.GetInt32(2);
bolNullFound = <span style="color:Blue; false;
<span style="color:Green; //str3 = Convert.ToString(i3);
}
<span style="color:Blue; else
bolNullFound = <span style="color:Blue; true;

++counter1; <span style="color:Green; // Console.WriteLine(reader.GetInt16(1));

<span style="color:Green; //Start of TEST
DataRow row = ds2.Tables[<span style="color:#A31515; "T1Test"].NewRow();
row[<span style="color:#A31515; "EventID"] = i1;
row[<span style="color:#A31515; "EventCode"] = i2;
<span style="color:Blue; if (bolNullFound == <span style="color:Blue; false)
{
row[<span style="color:#A31515; "DebtID"] = i3;
}
<span style="color:Blue; else
row[<span style="color:#A31515; "DebtID"].Equals(DBNull.Value);

ds2.Tables[<span style="color:#A31515; "T1Test"].Rows.Add(row);
}
[/code]
in C++ we have:
<div style="color:Black;background-color:White; <pre>
CDatabase cd;
CRecordset sourcers(&cd);
CString DSNString = _T(<span style="color:#A31515; "DSN=database;UID=sa;PWD=password;"),
SQLString = _T(<span style="color:#A31515; "SELECT EventID, EventCode, Debtid FROM Events WHERE Eventcode IN (10, 16)");

<span style="color:Blue; try
{
cd.OpenEx(DSNString, NULL );
}
<span style="color:Blue; catch(CDBException *e )
{
AfxMessageBox(_T(<span style="color:#A31515; "Didnt open ODBC database.n") + e->m_strError);
<span style="color:Blue; throw;
}

<span style="color:Blue; try
{
sourcers.Open( CRecordset::snapshot , SQLString );
}
<span style="color:Blue; catch(CDBException *e )
{
AfxMessageBox(_T(<span style="color:#A31515; "failed opening ODBC CRecordset ") + e->m_strError);
<span style="color:Blue; throw;
}

<span style="color:Blue; int fieldcount = sourcers.GetODBCFieldCount( ), i, rec;
CDBVariant varValue;
<span style="color:Green; //CString tstring;
<span style="color:Blue; long <span style="color:Blue; int tc = 0;

printf(<span style="color:#A31515; "Starting...n");
CTime starttime = CTime::GetCurrentTime();
<span style="color:Blue; if ( !sourcers.IsEOF() )
sourcers.MoveFirst();

<span style="color:Blue; while ( !sourcers.IsEOF())
{
<span style="color:Blue; for ( i = 0; i < fieldcount; ++ i ) <span style="color:Green; // do nothing other than load field into tstring
sourcers.GetFieldValue(i, varValue);
++tc;
<span style="color:Green; // } // end for rec

<span style="color:Blue; try
{
sourcers.MoveNext();
}
<span style="color:Blue; catch( CDBException *e )
{
AfxMessageBox( _T(<span style="color:#A31515; "MoveNext Failed: ") + e->m_strError);
<span style="color:Blue; throw;
}
} <span style="color:Green; // end while
[/code]
The C# takes around 1 second to complete for 85,000 records and the C++ is taking around 90 seconds for the same 85,000 records this is all being done over the network. Run locally its about 10 seconds for the C++ app.<br/>
Any ideas as to why this is so much slower? This is with Windows 2008 Server, and SQL Server 2000. Running this on our other network with Windows 2000 Server and SQL Server 2000 it runs in 12 - 15 seconds (C++ tested only not C#)

View the full article
 
Back
Top