StayInSync プロパティの例 (VJ++)

この例では、階層 Recordset 内の行へのアクセスを簡単にする StayInSync プロパティの機能を示します。

外側のループで、各 author の姓名、州、および ID が表示されます。各行に追加された Recordset Fields コレクションから取得され、StayInSync プロパティによって、親の Recordset が新しい行に移動するたびに、自動的に rstTitleAuthor に割り当てられます。内側のループで、追加されたレコードセットの各行から 4 つのフィールドが表示されます。

import com.ms.wfc.data.*;
import java.io.* ;
import com.ms.com.*;

public class StayInSyncX
{
     // The main entry point for the application.

    public static void main (String[] args)
    {
        StayInSyncX();
        System.exit(0);
    }

    // StayInSyncX function

    static void StayInSyncX()
    {

        // Define ADO Objects.
        Connection cnConn1 = null;
        Recordset rstAuthors = null;
        Recordset rstTitleAuthor = null;

        // Declarations.
        BufferedReader in = new 
            BufferedReader (new InputStreamReader(System.in));
        String strCnn = "DSN=Pubs;Provider=MSDataShape;" + 
            "Data Provider=MSDASQL;uid=sa;pwd=;";

        try
        {
            cnConn1 = new Connection();
            cnConn1.open(strCnn);
            rstAuthors = new Recordset();
            rstAuthors.setStayInSync(true);
            rstAuthors.open("SHAPE  {select * from Authors} " +
                     "APPEND ({select * from titleauthor}" +
                     "RELATE au_id TO au_id) AS chapTitleAuthor",
                    cnConn1,
                    AdoEnums.CursorType.STATIC,
                    AdoEnums.LockType.READONLY,
                    AdoEnums.CommandType.TEXT);

            Variant varRstTitleAuthor = rstAuthors.getFields().
                getItem("chapTitleAuthor").getValue();
            rstTitleAuthor =new Recordset(varRstTitleAuthor.toObject());
            int intCount =0;
            while(!rstAuthors.getEOF())
            {
                System.out.println("\n" +
                    rstAuthors.getField("au_fname").getString() + " " +
                    rstAuthors.getField("au_lname").getString() + " " +
                     rstAuthors.getField("state").getString() + " " +
                     rstAuthors.getField("au_id").getString());
                while(!rstTitleAuthor.getEOF())
                {
                    System.out.println(rstTitleAuthor.getField(0).
                        getString() + " " +
                        rstTitleAuthor.getField(1).getString() + " " +
                        rstTitleAuthor.getField(2).getString() + " " +
                        rstTitleAuthor.getField(3).getString());
                    rstTitleAuthor.moveNext();
                }
                if(++intCount % 5 == 0)
                {
                    System.out.println("\nPress <Enter> to continue..");
                    in.readLine();
                }
                rstAuthors.moveNext();
            }
            // Cleanup objects before exit.
            rstTitleAuthor.close();
            rstAuthors.close();
            cnConn1.close();

            System.out.println("\nPress <Enter> to continue..");
            in.readLine();
        }
        catch( AdoException ae )
        {
            // Notify user of any errors that result from ADO.

            // As passing a Recordset, check for null pointer first.
            if (rstAuthors != null)
            {
                PrintProviderError(rstAuthors.getActiveConnection());
            }
            else
            {
                System.out.println("Exception: " + ae.getMessage());
            }
        }

        // System read requires this catch.
        catch( java.io.IOException je)
        {
            PrintIOError(je);
        }
    }

    // PrintProviderError Function
    static void PrintProviderError(Connection cnn1)
    {
        // Print Provider Errors from Connection Object.
        // ErrItem is an item object in the Connection’s Errors Collection.
        com.ms.wfc.data.Error               ErrItem = null;
        long                                 nCount = 0;
        int                                       i = 0;

        nCount = cnn1.getErrors().getCount();

        // If there are any errors in the collection, print them.
        if ( nCount > 0)
        {
            // Collection ranges from 0 to nCount-1.
            for ( i=0;i<nCount; i++)
            {
                ErrItem = cnn1.getErrors().getItem(i);
                System.out.println("\t Error Number: " + ErrItem.getNumber() 
                    + "\t" + ErrItem.getDescription());
            }
        }
    }
    // PrintIOError Function
    static void PrintIOError(java.io.IOException je)
    {
        System.out.println("Error: \n");
        System.out.println("\t Source: " + je.getClass() + "\n");
        System.out.println("\t Description: "+ je.getMessage() + "\n");
    }
}