public static DataObjectInfo GetDataObjectInfo(string TableName, OleExec DB, DB_TYPE_ENUM DBType) { //先從靜態緩存中檢索,命中則返回,否則加載新的 string strTableName = TableName.ToUpper(); string strSql = ""; DataObjectInfo info = null; if (TableConfigs.ContainsKey(strTableName)) { info = (DataObjectInfo)TableConfigs[strTableName].Data; return(info); } info = new DataObjectInfo(); if (DBType == DB_TYPE_ENUM.Oracle) { //查詢Table元數據 strSql = $@"SELECT T1.OWNER, T1.TABLE_NAME, T1.COLUMN_NAME, T1.NULLABLE, T1.DATA_TYPE , T1.DATA_LENGTH , T2.COMMENTS FROM all_tab_cols T1, all_col_comments T2 WHERE T1.TABLE_NAME = T2.TABLE_NAME AND T1.COLUMN_NAME = T2.COLUMN_NAME AND T1.TABLE_NAME = '{strTableName}'"; DataSet res = DB.ExecSelect(strSql); for (int i = 0; i < res.Tables[0].Rows.Count; i++) { DataObjectColInfo CI = new DataObjectColInfo(); CI.name = res.Tables[0].Rows[i]["COLUMN_NAME"].ToString(); switch (res.Tables[0].Rows[i]["DATA_TYPE"].ToString()) { case "VARCHAR2": case "NVARCHAR2": case "CHAR": case "CLOB": CI.DataType = typeof(string); break; case "DATE": CI.DataType = typeof(DateTime); break; case "NUMBER": case "FLOAT": CI.DataType = typeof(double); break; case "LONG": CI.DataType = typeof(long); break; case "BLOB": CI.DataType = typeof(object); break; default: throw new Exception($@"Table:{strTableName} Col: {CI.name} dataType:{res.Tables[0].Rows[i]["DATA_TYPE"].ToString()} 不受支持"); } CI.length = double.Parse(res.Tables[0].Rows[i]["DATA_LENGTH"].ToString()); CI.nullable = res.Tables[0].Rows[i]["NULLABLE"].ToString() == "Y" ? true : false; info.BaseColsInfo.Add(CI); } DictionaryItem item = new DictionaryItem(); info.TableName = TableName; item.Data = info; TableConfigs.Add(TableName, item); return(info); } else { throw new Exception("數據庫類型:" + DBType.ToString() + "不受支持"); } }
//public public DataObjectTable(string _TableName, OleExec DB, DB_TYPE_ENUM DBType) { DataInfo = GetDataObjectInfo(_TableName, DB, DBType); TableName = _TableName; }