using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
namespace ExportOracleSchema
{
class ExportOracleSchemaConsole
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
OracleConnection cnn = new OracleConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"));
OracleCommand cmd = new OracleCommand();
OracleDataReader readerTables;
OracleDataReader readerColumns;
OracleDataReader readerComments;
OracleString comment;
OracleNumber precision;
OracleNumber length;
//
cnn.Open();
cmd.Connection = cnn;
//
cmd.CommandText = "select table_name from all_all_tables where owner = '" + ConfigurationSettings.AppSettings.Get("OracleSchema") + "' order by table_name";
cmd.CommandType = CommandType.Text;
readerTables = cmd.ExecuteReader();
//
Console.WriteLine("<?xml version='1.0'?>");
Console.WriteLine("<?xml-stylesheet type=\"text/xsl\" href=\"oraschema.xslt\"?>");
Console.WriteLine("<tables>");
//
while (readerTables.Read())
{
//
string tablename = readerTables.GetString(0);
//
Console.WriteLine("<table id=\"" + tablename + "\" >");
Console.WriteLine("<columns>");
//
//
cmd.CommandText = " select ALL_TAB_COLUMNS.COLUMN_NAME, COLUMN_ID, " +
"DATA_DEFAULT, NULLABLE, " +
" DATA_TYPE, DATA_PRECISION, " +
" COMMENTS, DATA_LENGTH " +
" from ALL_TAB_COLUMNS, All_Col_Comments " +
" where ALL_TAB_COLUMNS.OWNER = ALL_COL_COMMENTS.Owner AND " +
" ALL_TAB_COLUMNS.TABLE_NAME = ALL_COL_COMMENTS.TABLE_NAME AND " +
" ALL_TAB_COLUMNS.COLUMN_NAME = all_col_comments.column_name and " +
" ALL_TAB_COLUMNS.OWNER = '" + ConfigurationSettings.AppSettings.Get("OracleSchema") + "' and ALL_TAB_COLUMNS.TABLE_NAME = '" + tablename + "' ";
readerColumns = cmd.ExecuteReader();
//
while( readerColumns.Read() )
{
//
comment = readerColumns.GetOracleString(6);
precision = readerColumns.GetOracleNumber(5);
length = readerColumns.GetOracleNumber(7);
Console.WriteLine("<column>");
Console.WriteLine("<name>" + readerColumns.GetString(0) + "</name>");
Console.WriteLine("<type>" + readerColumns.GetString(4) + "</type>");
Console.WriteLine("<length>" + (precision.IsNull?length.ToString():precision.ToString()) + "</length>");
Console.WriteLine("<nullable>" + readerColumns.GetString(3) + "</nullable>");
Console.WriteLine("<comment>" + (comment.IsNull?"":comment.ToString()) + "</comment>");
Console.WriteLine("</column>");
//
}
//
Console.WriteLine("</columns>");
//
readerColumns.Close();
//
cmd.CommandText = "select COMMENTS from all_tab_comments where owner = '" + ConfigurationSettings.AppSettings.Get("OracleSchema") + "' and all_tab_comments.table_name='" + tablename + "'";
readerComments = cmd.ExecuteReader();
while( readerComments.Read() )
{
//
comment = readerComments.GetOracleString(0);
Console.WriteLine("<comments>");
Console.WriteLine(comment.IsNull?"":comment.ToString());
Console.WriteLine("</comments>");
//
}
//
readerComments.Close();
//
Console.WriteLine("</table>");
//
}
//
Console.WriteLine("</tables>");
//
readerTables.Close();
cnn.Close();
//
}
}
}