Using ADO.Net to Read and Update an External Table

shrekutshrekut Member Posts: 21
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
      [email protected] : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlConnection";
      [email protected] : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlCommand";
      [email protected] : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlDataReader";
      [email protected] : Text[1000];
      [email protected] : Record 311;
      [email protected] : Date;
      [email protected] : Text[250];
      [email protected] : Text[250];
      [email protected] : Text[250];

    EVENT [email protected]::[email protected]([email protected] : Variant;[email protected] : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.StatementCompletedEventArgs");
    BEGIN
    END;

    EVENT [email protected]::[email protected]([email protected] : Variant;[email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.EventArgs");
    BEGIN
    END;

    EVENT [email protected]::[email protected]([email protected] : Variant;[email protected] : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.SqlClient.SqlInfoMessageEventArgs");
    BEGIN
    END;

    EVENT [email protected]::[email protected]([email protected] : Variant;[email protected] : DotNet "'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.StateChangeEventArgs");
    BEGIN
    END;

    EVENT [email protected]::[email protected]([email protected] : Variant;[email protected] : 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.