public List<TableOrViewInfo> ReadAllTableAndViewInfo(string connString) { // 预期结果. List<TableOrViewInfo> resultList = new List<TableOrViewInfo>(); using (OracleConnection conn = new OracleConnection(connString)) { // 创建DataSet,用于存储数据. DataSet resultDataSet = new DataSet(); // 创建一个适配器 OracleDataAdapter tableAdapter = new OracleDataAdapter(READ_TABLE_INFO_SQL, conn); // 执行查询,并将数据导入DataSet. tableAdapter.Fill(resultDataSet, "TableInfo"); // 创建一个适配器 OracleDataAdapter tableColumnAdapter = new OracleDataAdapter(READ_TABLE_COLUMN_INFO_SQL, conn); // 执行查询,并将数据导入DataSet. tableColumnAdapter.Fill(resultDataSet, "TableColumnInfo"); #region 先加表. foreach (DataRow tableRow in resultDataSet.Tables["TableInfo"].Rows) { // 依次处理每一个表. TableOrViewInfo tableInfo = new TableOrViewInfo(); // 表. tableInfo.IsView = false; // 表名. tableInfo.Name = tableRow["TABLE_NAME"].ToString(); // 备注. tableInfo.Comment = tableRow["COMMENTS"].ToString(); // 列. tableInfo.ColumnList = new List<ColumnInfo>(); // 检索当前表的列. DataRow[] rows = resultDataSet.Tables["TableColumnInfo"].Select("TABLE_NAME='" + tableInfo.Name + "'"); for (int i = 0; i < rows.Length; i++) { DataRow row = rows[i]; // 读取每一列. ColumnInfo columnInfo = new ColumnInfo(); // 列名 columnInfo.Name = row["COLUMN_NAME"].ToString(); // 备注/说明信息 columnInfo.Comment = row["COMMENTS"].ToString(); // 列加入表的列List. tableInfo.ColumnList.Add(columnInfo); } // 处理完毕后,表加入结果列表. resultList.Add(tableInfo); } #endregion } return resultList; }
public List<TableOrViewInfo> ReadAllTableAndViewInfo(string connString) { // 预期结果. List<TableOrViewInfo> resultList = new List<TableOrViewInfo>(); // 建立数据库连接. SqlConnection conn = new SqlConnection(connString); // 创建DataSet,用于存储数据. DataSet resultDataSet = new DataSet(); // 创建一个适配器 SqlDataAdapter tableAdapter = new SqlDataAdapter(READ_TABLE_INFO_SQL, conn); // 执行查询,并将数据导入DataSet. tableAdapter.Fill(resultDataSet, "TableInfo"); // 创建一个适配器 SqlDataAdapter tableColumnAdapter = new SqlDataAdapter(READ_TABLE_COLUMN_INFO_SQL, conn); // 执行查询,并将数据导入DataSet. tableColumnAdapter.Fill(resultDataSet, "TableColumnInfo"); // 创建一个适配器 SqlDataAdapter viewAdapter = new SqlDataAdapter(READ_VIEW_INFO_SQL, conn); // 执行查询,并将数据导入DataSet. viewAdapter.Fill(resultDataSet, "ViewInfo"); // 创建一个适配器 SqlDataAdapter viewColumnAdapter = new SqlDataAdapter(READ_VIEW_COLUMN_INFO_SQL, conn); // 执行查询,并将数据导入DataSet. viewColumnAdapter.Fill(resultDataSet, "ViewColumnInfo"); #region 先加表. foreach (DataRow tableRow in resultDataSet.Tables["TableInfo"].Rows) { // 依次处理每一个表. TableOrViewInfo tableInfo = new TableOrViewInfo(); // 表. tableInfo.IsView = false; // 表名. tableInfo.Name = tableRow["TableName"].ToString(); // 备注. tableInfo.Comment = tableRow["Comment"].ToString(); // 列. tableInfo.ColumnList = new List<ColumnInfo>(); // 检索当前表的列. DataRow[] rows = resultDataSet.Tables["TableColumnInfo"].Select("TableName='" + tableInfo.Name + "'", "ColumnId"); for (int i = 0; i < rows.Length; i++) { DataRow row = rows[i]; // 读取每一列. ColumnInfo columnInfo = new ColumnInfo(); // 列名 columnInfo.Name = row["ColumnName"].ToString(); //// 列的顺序 //columnInfo.ColumnId = Convert.ToInt32(row["ColumnId"]); //// 数据类型 //columnInfo.DataType = row["DataType"].ToString(); //// 占用字节数 //columnInfo.MaxLength = Convert.ToInt32(row["MaxLength"]); //// 长度 //columnInfo.Precision = Convert.ToInt32(row["Precision"]); //// 小数位数 //columnInfo.Scale = Convert.ToInt32(row["Scale"]); //// 是否允许非空 //columnInfo.IsNullAble = Convert.ToInt32(row["IsNullAble"]) == 1; //// 是否是自增ID //columnInfo.IsIDEntity = Convert.ToInt32(row["IsIDEntity"]) == 1; //// 是否是主键 //columnInfo.IsPrimaryKey = Convert.ToInt32(row["IsPrimaryKey"]) == 1; // 备注/说明信息 columnInfo.Comment = row["Comment"].ToString(); // 列加入表的列List. tableInfo.ColumnList.Add(columnInfo); } // 处理完毕后,表加入结果列表. resultList.Add(tableInfo); } #endregion #region 后处理视图. foreach (DataRow viewRow in resultDataSet.Tables["ViewInfo"].Rows) { // 依次处理每一个视图. TableOrViewInfo viewInfo = new TableOrViewInfo(); // 视图. viewInfo.IsView = true; // 视图名. viewInfo.Name = viewRow["ViewName"].ToString(); // 备注. viewInfo.Comment = viewRow["Comment"].ToString(); // 列. viewInfo.ColumnList = new List<ColumnInfo>(); // 检索当前表的列. DataRow[] rows = resultDataSet.Tables["ViewColumnInfo"].Select("ViewName='" + viewInfo.Name + "'", "ColumnId"); for (int i = 0; i < rows.Length; i++) { DataRow row = rows[i]; // 读取每一列. ColumnInfo columnInfo = new ColumnInfo(); // 列名 columnInfo.Name = row["ColumnName"].ToString(); //// 列的顺序 //columnInfo.ColumnId = Convert.ToInt32(row["ColumnId"]); //// 数据类型 //columnInfo.DataType = row["DataType"].ToString(); //// 占用字节数 //columnInfo.MaxLength = Convert.ToInt32(row["MaxLength"]); //// 长度 //columnInfo.Precision = Convert.ToInt32(row["Precision"]); //// 小数位数 //columnInfo.Scale = Convert.ToInt32(row["Scale"]); //// 是否允许非空 //columnInfo.IsNullAble = Convert.ToInt32(row["IsNullAble"]) == 1; //// 是否是自增ID //columnInfo.IsIDEntity = Convert.ToInt32(row["IsIDEntity"]) == 1; //// 是否是主键 //columnInfo.IsPrimaryKey = Convert.ToInt32(row["IsPrimaryKey"]) == 1; // 备注/说明信息 columnInfo.Comment = row["Comment"].ToString(); // 列加入 视图的列List. viewInfo.ColumnList.Add(columnInfo); } // 处理完毕后,加入结果列表. resultList.Add(viewInfo); } #endregion return resultList; }