I use this code to load data from SAP DI (SQL Query) to a DataTable:
namespace SAPApp
{
class SAPData
{
public void GetSAPData(Company oCompany)
{
if (oCompany.Connected)
{
dt.TableName = _TableName;
DataColumn dtColumn;
DataRow dtRow;
Cursor.Current = Cursors.WaitCursor;
SAPbobsCOM.Recordset oRecordSet;
oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
oRecordSet.DoQuery(SQL);
oRecordSet.MoveFirst();
dt.BeginLoadData();
int colCount = oRecordSet.Fields.Count;
for (int i = 0; i < colCount; i++)
{
string colName = oRecordSet.Fields.Item(i).Name;
BoFieldTypes ftype = oRecordSet.Fields.Item(i).Type;
var dType = Type.GetType("System.String");
if (ftype == BoFieldTypes.db_Date)
dType = Type.GetType("System.DateTime");
else if (ftype == BoFieldTypes.db_Numeric)
dType = Type.GetType("System.Decimal");
dtColumn = new DataColumn()
{
DataType = dType,
ColumnName = colName,
Caption = colName,
};
dt.Columns.Add(dtColumn);
}
while (!oRecordSet.EoF)
{
dtRow = dt.NewRow();
for (int i = 0; i < colCount; i++)
dtRow[i] = oRecordSet.Fields.Item(i).Value;
dt.Rows.Add(dtRow);
oRecordSet.MoveNext();
}
dv = new DataView(dt);
dt.EndLoadData();
Cursor.Current = Cursors.Default;
}
}
Works fine!. It takes 0.222 sec to execute when the SAPbobsCOM.company & Company.Connect() are defined "locally" in Main Form (single form app). But when I move the SAPbobsCOM.company object to an "external" class the same code run on +22 secs (Moving app to MDI).
Local declaration:
namespace SAPApp
{
public partial class FrmMain : Form
{
private readonly Company oCompany = new Company();
...
// Call in main form
SAPData SDAcc = new SAPData;
SDAcc.GetSAPData(oCompany); // param: oCompany object
External Declaration:
\\ First create a TSapDI with oCompany objet
namespace SAPApp.Commons
{
public partial class TSapDI
{
public readonly Company oCompany = new Company();
\\ Then instantiate in Globals
namespace SAPApp.Commons
{
internal class Globals
{
public static TSapDI SapDI = new TSapDI();
\\ Create method in Commons
namespace SAPApp
{
class SAPData
{
public void GetSAPData(Company oCompany)....
\\ Finally call it
SAPData SDSearch = new SAPData
SDSearch.GetSAPData(Globals.SapDI.oCompany); // param: Globals.SapDI.oCompany object
In first case VisualStudio can inspect oCompany object inside GetSAPData but in second can't.
Can someone help me understand why such a difference and if possible how to fix it? Thanks.
P.S. I think it has to do with oCompany changing from a static to a non-static object.
I need the code to be executed in less than 1 sec.