LoadFromOracle函數用於 分析Oracle數據庫而獲得表結構和字段設計信息。其代碼如下。
在Oracle數據庫中, 有一個名為COL的系統預定義視圖,裡面就是各個數據表名和字段定義信息,還有一個名為 user_ind_columns的預定義視圖,裡面就保存著字段索引信息。
我們首先查詢遍歷 COL視圖,獲得該視圖中保存的數據表名,字段名,字段數據類型,字段長度等信息。建立起 基本的表和字段信息結構,然後查詢遍歷user_ind_columns視圖,獲得其關鍵字段信息。
/// <summary>
/// 從 Oracle 加載數據庫結構信息
/// </summary>
/// <param name="myConn">數據庫連接對象 </param>
/// <returns>加載的字段信息個數</returns>
public int LoadFromOracle( IDbConnection myConn )
{
intFillStyle = FillStyleConst.Oracle ;
int RecordCount = 0 ;
string strSQL = null;
strSQL = "Select TName,CName,coltype,width From Col Order by TName,CName";
myTables.Clear();
if( myConn is OleDbConnection )
{
strName = ( ( System.Data.OleDb.OleDbConnection ) myConn ).DataSource
+ " - " + myConn.Database ;
}
else
strName = myConn.Database ;
using( System.Data.IDbCommand myCmd = myConn.CreateCommand())
{
myCmd.CommandText = strSQL ;
IDataReader myReader = myCmd.ExecuteReader( CommandBehavior.SingleResult );
TableInfo LastTable = null;
while( myReader.Read())
{
string TableName = myReader.GetString(0).Trim();
if( LastTable == null || LastTable.Name != TableName )
{
LastTable = new TableInfo();
myTables.Add( LastTable );
LastTable.Name = TableName ;
}
FieldInfo NewField = new FIEldInfo();
LastTable.Fields.Add( NewFIEld );
NewFIEld.Name = myReader.GetString(1);
NewField.FIEldType = myReader.GetString(2);
NewField.FIEldWidth = myReader[3].ToString();
RecordCount ++ ;
}//while
myReader.Close();
myCmd.CommandText = @"
select table_name ,
column_name ,
index_name
from user_ind_columns
order by table_name , column_name ";
myReader = myCmd.ExecuteReader( CommandBehavior.SingleResult );
TableInfo myTable = null;
while( myReader.Read())
{
myTable = myTables[ myReader.GetString(0)];
if( myTable != null )
{
string IDName = myReader.GetString(2);
string FIEldName = myReader.GetString(1);
FieldInfo myField = myTable.Fields[ FIEldName ];
if( myFIEld != null )
{
myFIEld.Indexed = true ;
if( IDName.StartsWith("PK") )
{
myFIEld.PrimaryKey = true;
}
}
}
}//while
myReader.Close ();
}//using
return RecordCount ;
}//public int LoadFromOracle( System.Data.IDbConnection myConn )