Using an ALIAS to access a multi-member file
To use an ALIAS to access a member other than the first member of a file, first create the
alias, and then use that ALIAS instead of the file name. Example 4-97 shows how to create
an alias to the second member of file MYFILE, then use that alias to reference the member.
Example 4-97 Accessing a multi-member file using an ALIAS
iDB2Command cmd = new iDB2Command("create alias myschema.fileMbr2 for
myschema.myfile(member2)", cn);
cmd.ExecuteNonQuery();
// Now access the second member using the alias we just created
cmd.CommandText = "select * from myschema.fileMbr2";
iDB2DataReader dr = cmd.ExecuteReader();
// Etc.
Using OVRDBF to access a multi-member file
You can use the OVRDBF command to temporarily override a multiple-member database file
as shown in Example 4-98. Using the CallPgm example we create in “A general method for
invoking QCMDEXC” on page 120, we override the file so that SQL will reference MEMBER2
instead of the first member in the file. After calling OVRDBF as we show in this example, SQL
statements that reference the file MYSCHEMA.MYFILE use the second member, MEMBER2.
Example 4-98 Using OVRDBF to access the second member of a multi-member file
rc = CallPgm("OVRDBF FILE(MYFILE) TOFILE(MYSCHEMA/MYFILE) MBR(MEMBER2) OVRSCOPE(*JOB)",
cn);
// Now a SELECT * FROM MYSCHEMA.MYFILE will access member MEMBER2
cmd.CommandText = "select * from myschema.myfile";
iDB2DataReader dr = cmd.ExecuteReader();
// Etc.
Processing CL commands that produce an OUTFILE
Many CL commands provide an option to store the command output into an outfile, which is a
database file that can be queried using a SQL select statement, just like any other table. In
Example 4-99 on page 124, we execute a DSPUSRPRF command through QCMDEXC using
the CallPgm method we created in Example 4-94 on page 121. After running this command,
we execute a SELECT statement to select all of the user profiles from the table (outfile) that
was created by DSPUSRPRF. We use a DataReader to read information from the table.
124 Integrating DB2 Universal Database for iSeries with Microsoft ADO .NET
Example 4-99 Processing a CL command that produces an OUTFILE
// Create and open a connection to the iSeries.
iDB2Connection cn = new
iDB2Connection("DataSource=myiseries;DefaultCollection=sampledb;");
cn.Open();
// Call the CallPgm method to execute the DSPUSRPRF command
// and tell it to put the output into an OUTFILE.
bool success = CallPgm("DSPUSRPRF USRPRF(*ALL) OUTPUT(*OUTFILE)
OUTFILE(SAMPLEDB/USRPRFINFO)", cn);
// If the call succeeded, open a DataReader to read a list of
// all the user profiles in the outfile.
if (success == true)
{
iDB2Command cmd = new iDB2Command("SELECT UPUPRF FROM USRPRFINFO", cn);
iDB2DataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine("User profile: " + dr.GetString(0));
}
// Close the DataReader since we're done using it.
dr.Close();
// Dispose the command since we no longer need it.
cmd.Dispose();
}
// Close the connection since we're done using it.
cn.Close();