Using ADO.Net to Read and Update an External Table

shrekutshrekut Member Posts: 27
edited 2014-11-26 in SQL Tips & Tricks
I am trying to read and update a table in an external SQL Server. I am able to read the data ok, but I have not been able to figure out how to update the record I read from that table. I am including the Code Example I'm using to read. The statement to update the record is also included. Can someone please help me with an example of code that will execute the update statement?
OBJECT Codeunit 50006 ADO.Net External Table
{
  OBJECT-PROPERTIES
  {
    Date=11/25/14;
    Time=[ 9:23:03 AM];
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            SRSetup.GET;
            CLEAR(SQLReader);
            CLEAR(SQLCommand);
            CLEAR(SQLConnection);

            SQLConnection := SQLConnection.SqlConnection('Server=' + SRSetup."SQL Server Name" + ';'
                                                         + 'Database=' + SRSetup."SQL Database Name" + ';'
                                                         + 'User ID=' + SRSetup."SQL Login ID" + ';'
                                                         + 'Password=' + SRSetup."SQL Password" + ';');


            SQLString := 'SELECT * FROM tNavisionOrders WHERE ImportStatus = ' + '''' + 'Pending' + '''' + ';';
            SQLConnection.Open;
            SQLCommand := SQLConnection.CreateCommand();
            SQLCommand.CommandText := SQLString;
            SQLReader := SQLCommand.ExecuteReader;

            SQLReader.Read();
            DateStr := SQLReader.GetSqlValue(1);
            EVALUATE(TestDate,COPYSTR(DateStr,3,2) + '/' + COPYSTR(DateStr,1,2) + '/' + COPYSTR(DateStr,5,4));

            RemoteRefNo := SQLReader.GetSqlValue(0);

            ImportStatus := SQLReader.GetSqlValue(46);

            MESSAGE( 'Reading %1 || %2 || %3',RemoteRefNo,TestDate,ImportStatus);


            SQLString := 'UPDATE tNavisionOrders SET ImportStatus = ' + '''' + 'Imported' + ''''
                       + ' WHERE ImportStatus = ' + '''' + 'Pending' + ''''
                       + ' AND OrderNo = ' + '''' + RemoteRefNo + '''' + ';';

            // Need code to Update the above record (RemoteRefNo) with ImportStatus := 'Imported'

            SQLConnection.Close;
          END;

  }
  CODE
  {
    VAR
      SQLConnection@1000000009 : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlConnection";
      SQLCommand@1000000008 : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlCommand";
      SQLReader@1000000007 : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlDataReader";
      SQLString@1000000005 : Text[1000];
      SRSetup@1000000004 : Record 311;
      TestDate@1000000003 : Date;
      DateStr@1000000002 : Text[250];
      RemoteRefNo@1000000001 : Text[250];
      ImportStatus@1000000000 : Text[250];

    EVENT SQLCommand@1000000008::StatementCompleted@57(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.StatementCompletedEventArgs");
    BEGIN
    END;

    EVENT SQLCommand@1000000008::Disposed@58(sender@1000000001 : Variant;e@1000000000 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.EventArgs");
    BEGIN
    END;

    EVENT SQLConnection@1000000009::InfoMessage@48(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlInfoMessageEventArgs");
    BEGIN
    END;

    EVENT SQLConnection@1000000009::StateChange@49(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.StateChangeEventArgs");
    BEGIN
    END;

    EVENT SQLConnection@1000000009::Disposed@50(sender@1000000001 : Variant;e@1000000000 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.EventArgs");
    BEGIN
    END;

    BEGIN
    END.
  }
}

Comments

  • tinoruijstinoruijs Member Posts: 1,226
    I think you're missing:
    SQLConnection.Execute(SQLString);
    
    befor SQLConnection.Close;

    Tino Ruijs
    Microsoft Dynamics NAV specialist
Sign In or Register to comment.