public class ColumnEntity
{public ColumnEntity()
{
}
public string ColumnOwner { get; set; }
public string ColumnTableName { get; set; }
public string ColumnName { get; set; }
public string ColumnType { get; set; }
public string ColumnLength { get; set; }
public string ColumnPrecision { get; set; }
public string ColumnScale { get; set; }
public string ColumnNULLABLE { get; set; }
public string ColumnID { get; set; }
public string ColumnDefault { get; set; }
public ColumnEntity Clone()
{
return this.MemberwiseClone() as ColumnEntity;
}
}
public class TableEntity
{
public TableEntity()
{
}
public string TableName { get; set; }
private Dictionary<string, ColumnEntity> columns = new Dictionary<string, ColumnEntity>();
public Dictionary<string, ColumnEntity> Columns
{
get { return columns; }
set { this.columns = value; }
}
}
public class DataBase
{
public DataBase(string connString)
{
this.connString = connString;
}
private string connString = "";
private OracleConnection connection;
public OracleDataReader ExeSql(string sql)
{
connection = new OracleConnection(connString);
OracleCommand command = new OracleCommand(sql);
command.Connection = connection;
try
{
connection.Open();
return command.ExecuteReader();
}
catch (Exception ex)
{
return null;
}
}
public bool CloseConnection()
{
connection.Close();
return true;
}
}
public partial class frmTools : Form
{
public frmTools()
{
InitializeComponent();
}
private void btnTest_Click(object sender, EventArgs e)
{
string connString = string.Format("Data Source={0};user={1};password={2}", "orcl111", "his", "his");
DataBase db = new DataBase(connString);
string sql = @"SELECT OWNER,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
DATA_PRECISION,
DATA_SCALE,
NULLABLE,
COLUMN_ID,
DATA_DEFAULT
FROM ALL_TAB_COLUMNS C
WHERE C.OWNER = 'HIS'
AND (C.TABLE_NAME = 'ALL' OR 'ALL' = 'ALL')
ORDER BY OWNER,TABLE_NAME,COLUMN_ID";
OracleDataReader reader = db.ExeSql(sql);
if (reader == null)
{
MessageBox.Show("NO data");
return;
}
List<TableEntity> lstTables = new List<TableEntity>();
TableEntity tableEntity = null;
string tabName = string.Empty;
while (reader.Read())
{
if (tabName == string.Empty)
{
tableEntity = new TableEntity();
tabName = reader["TABLE_NAME"].ToString();
tableEntity.TableName = tabName;
}
else if (tabName != reader["TABLE_NAME"].ToString())
{
lstTables.Add(tableEntity);
tableEntity = new TableEntity();
tabName = reader["TABLE_NAME"].ToString();
tableEntity.TableName = tabName;
}
ColumnEntity colEntity = new ColumnEntity();
colEntity.ColumnOwner = reader["OWNER"].ToString();
colEntity.ColumnTableName = reader["TABLE_NAME"].ToString();
colEntity.ColumnName = reader["COLUMN_NAME"].ToString();
colEntity.ColumnType = reader["DATA_TYPE"].ToString();
colEntity.ColumnLength = reader["DATA_LENGTH"].ToString();
colEntity.ColumnPrecision = reader["DATA_PRECISION"].ToString();
colEntity.ColumnScale = reader["DATA_SCALE"].ToString();
colEntity.ColumnNULLABLE = reader["NULLABLE"].ToString();
colEntity.ColumnID = reader["COLUMN_ID"].ToString();
colEntity.ColumnDefault = reader["DATA_DEFAULT"].ToString();
tableEntity.Columns.Add(colEntity.ColumnName, colEntity);
}
if (tabName != null)
{
lstTables.Add(tableEntity);
}
reader.Close();
db.CloseConnection();
int spdRowIdx = 0;
int spdColIdx = 0;
spdData_View.Rows.Count = 0;
foreach (TableEntity entity in lstTables)
{
foreach (var item in entity.Columns)
{
this.spdData_View.Rows.Count++;
spdColIdx = 0;
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnOwner);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnTableName);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnName);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnType);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnLength);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnPrecision);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnScale);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnNULLABLE);
spdData_View.SetValue(spdRowIdx, spdColIdx++, item.Value.ColumnID);
spdData_View.SetValue(spdRowIdx++, spdColIdx++, item.Value.ColumnDefault);
}
}
}
}