/****************************************************************************
** (c) Copyright IBM Corp. 2007 All rights reserved.
**
** The following sample of source code ("Sample") is owned by International
** Business Machines Corporation or one of its subsidiaries ("IBM") and is
** copyrighted and licensed, not sold. You may use, copy, modify, and
** distribute the Sample in any form without payment to IBM, for the purpose of
** assisting you in the development of your applications.
**
** The Sample code is provided to you on an "AS IS" basis, without warranty of
** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
** not allow for the exclusion or limitation of implied warranties, so the above
** limitations or exclusions may not apply to you. IBM shall not be liable for
** any damages you suffer as a result of using, copying, modifying or
** distributing the Sample, even if IBM has been advised of the possibility of
** such damages.
*****************************************************************************
**
** SOURCE FILE NAME: TbInfo.cs
**
** SAMPLE: How to get information about a table
** with the DB2 .Net Data Provider
**
** SQL Statements USED:
** SELECT
**
** DB2 .NET Data Provider Classes USED:
** DB2Connection
** DB2Command
**
**
*****************************************************************************
**
** Building and Running the sample program
**
** 1. Compile the TbInfo.cs file with bldapp.bat by entering the following
** at the command prompt:
**
** bldapp TbInfo
**
** or compile TbInfo.cs with the makefile by entering the following at
** the command prompt:
**
** nmake TbInfo
**
** 2. Run the TbInfo program by entering the program name at the command
** prompt:
**
** TbInfo
**
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing applications, see the Application
** Development Guide.
**
** For information on using SQL statements, see the SQL Reference.
**
** For the latest information on programming, compiling, and running DB2
** applications, visit the DB2 Information Center at
** http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
**
****************************************************************************/
using System;
using System.Data;
using System.IO;
using IBM.Data.DB2;
class TbInfo
{
public static void Main(String[] args)
{
// Declare a DB2Connection
DB2Connection conn = null;
try
{
Console.WriteLine();
Console.WriteLine(
" THIS SAMPLE SHOWS HOW TO GET INFORMATION ABOUT A TABLE.");
// Declare a String to store the table name
String tableName;
// Connect to a database
Console.WriteLine("\n Connecting to a database ...");
conn = ConnectDb(args);
tableName = "STAFF";
// Obtain the schema name for a table
GetSchemaName(conn, tableName);
// Obtain the column information for a table
GetColumnInfo(conn, tableName);
// Disconnect from the database
Console.WriteLine("\n Disconnect from the database.");
conn.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.Close();
}
} // Main
// Helper method: This method establishes a connection to a database
public static DB2Connection ConnectDb(String[] argv)
{
String server = "";
String alias = "";
String userId = "";
String password = "";
Int32 portNumber = -1;
String connectString;
if( argv.Length > 5 ||
( argv.Length == 1 &&
( String.Compare(argv[0],"?") == 0 ||
String.Compare(argv[0],"-?") == 0 ||
String.Compare(argv[0],"/?") == 0 ||
String.Compare(argv[0],"-h",true) == 0 ||
String.Compare(argv[0],"/h",true) == 0 ||
String.Compare(argv[0],"-help",true) == 0 ||
String.Compare(argv[0],"/help",true) == 0 ) ) )
{
throw new Exception(
"Usage: prog_name [dbAlias] [userId passwd] \n" +
" prog_name [dbAlias] server portNum userId passwd");
}
switch (argv.Length)
{
case 0: // Use all defaults
alias = "sample";
userId = "";
password = "";
break;
case 1: // dbAlias specified
alias = argv[0];
userId = "";
password = "";
break;
case 2: // userId & passwd specified
alias = "sample";
userId = argv[0];
password = argv[1];
break;
case 3: // dbAlias, userId & passwd specified
alias = argv[0];
userId = argv[1];
password = argv[2];
break;
case 4: // use default dbAlias
alias = "sample";
server = argv[0];
portNumber = Convert.ToInt32(argv[1]);
userId = argv[2];
password = argv[3];
break;
case 5: // everything specified
alias = argv[0];
server = argv[1];
portNumber = Convert.ToInt32(argv[2]);
userId = argv[3];
password = argv[4];
break;
}
if(portNumber==-1)
{
connectString = "Database=" + alias;
}
else
{
connectString = "Server=" + server + ":" + portNumber +
";Database=" + alias;
}
if(userId != "")
{
connectString += ";UID=" + userId + ";PWD=" + password;
}
DB2Connection conn = new DB2Connection(connectString);
conn.Open();
Console.WriteLine(" Connected to the " + alias + " database");
return conn;
} // ConnectDb
// This method demonstrates how to get the schema name for a table
public static void GetSchemaName(DB2Connection conn, String tableName)
{
Console.WriteLine();
Console.WriteLine(
" ----------------------------------------------------------\n" +
" USE THE SQL STATEMENT:\n" +
" SELECT\n" +
" TO GET THE SCHEMA NAME OF A TABLE.");
try
{
// Get the schema name for a table and display it
Console.WriteLine();
Console.WriteLine(
" Execute the statement:\n" +
" SELECT tabschema\n" +
" FROM syscat.tables\n" +
" WHERE tabname = '" + tableName + "'");
DB2Command cmd = conn.CreateCommand();
cmd.CommandText = "SELECT tabschema " +
" FROM syscat.tables " +
" WHERE tabname = '" + tableName + "'";
DB2DataReader reader = cmd.ExecuteReader();
reader.Read();
Console.WriteLine();
Console.WriteLine(" Table schema name is: " + reader.GetString(0));
reader.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} // GetSchemaName
// This method demonstrates how to get the column information for a table
public static void GetColumnInfo(DB2Connection conn, String tableName)
{
String dataColname = "";
String dataTypename = "";
Int32 dataLength = 0;
Int32 dataScale = 0;
Console.WriteLine();
Console.WriteLine(
" ----------------------------------------------------------\n" +
" USE THE SQL STATEMENTS:\n" +
" SELECT\n" +
" TO GET THE COLUMN INFORMATION OF A TABLE.");
try
{
// Get the column information for a table
Console.WriteLine();
Console.WriteLine(
" The following SQL statement gets the column information \n" +
" of the '" + tableName + "' table: \n");
Console.WriteLine(
" SELECT colname, typename, length, scale \n" +
" FROM syscat.columns \n" +
" WHERE tabname = '" + tableName + "'\n");
Console.WriteLine(
" column name data type data size\n" +
" -------------------- -------------- ----------");
// Create a DB2Command to execute the SQL statement
DB2Command cmd = conn.CreateCommand();
cmd.CommandText = "SELECT colname, typename, length, scale " +
" FROM syscat.columns " +
" WHERE tabname = '" + tableName + "'\n";
DB2DataReader reader = cmd.ExecuteReader();
// Check if the query returned no data
if (reader.Read() == false)
{
Console.WriteLine();
Console.WriteLine(" Data not found.\n");
}
// Retrieve and display the column information from the DB2DataReader
do
{
dataColname = reader.GetString(0);
dataTypename = reader.GetString(1);
dataLength = reader.GetInt32(2);
dataScale = reader.GetInt16(3);
Console.Write(" " + dataColname.PadRight(20) +
" " + dataTypename.PadRight(14) +
" " + dataLength);
if (dataScale != 0)
{
Console.WriteLine("," + dataScale);
}
else
{
Console.WriteLine();
}
} while (reader.Read());
reader.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} // GetColumnInfo
} // TbInfo