/// <summary> /// 返回指定名称的架构信息 /// </summary> /// <param name="collectionName">集合名称</param> /// <returns>DataTable</returns> public static DataTable GetSchema(string collectionName) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataTable dt = new DataTable(); try { dt.Clear(); connection.Open(); dt = connection.GetSchema(collectionName); } catch { dt = null; } return dt; } }
public DbConnection Connection(DatabaseType dbT, string cs) { DbConnection c = null; switch (dbT) { case DatabaseType.SQLite: c = new SQLiteConnection(cs); break; case DatabaseType.Access: c = new OleDbConnection(cs); break; case DatabaseType.Oracle: c = new OracleConnection(cs); break; default: break; } try { c.Open(); c.GetSchema("Tables"); } catch { return null; } return c; }
private DataTable GetSchema_OracleDA() { OracleConnection conn; string connetionString = GetConnectionString_OracleDA(); conn = new OracleConnection(connetionString); try { conn.Open(); DataTable schemaTable = conn.GetSchema(); return schemaTable; } catch (OracleException ex) { MessageBox.Show("Exception in GetSchema_OracleDA - " + ex.Message); } finally { conn.Close(); } return null; }
/// <summary> /// 获取架构信息 /// </summary> /// <param name="connectionString"></param> /// <param name="collectionName"></param> /// <param name="restrictionValues"></param> /// <returns></returns> public static DataTable GetSchema(string connectionString, string collectionName, string[] restrictionValues) { OracleConnection connection = new OracleConnection(connectionString); DataTable schema = new DataTable(); try { connection.Open(); if (!string.IsNullOrEmpty(collectionName)) { if (restrictionValues != null && restrictionValues != null && restrictionValues.Length > 0) { schema = connection.GetSchema(collectionName, restrictionValues); } else { schema = connection.GetSchema(collectionName); } } else { schema = connection.GetSchema(); } } catch { schema = null; } finally { connection.Close(); } return schema; }
private void OracleGetShcema() { try { using (var conn = new OracleConnection(txtOrclConn.Text)) { conn.Open(); string[] restriction = new string[] { txtOrclOwner.Text }; DataTable schema; if (txtOrclSchemaName.Text.ToLower() == "datatypes") { schema = conn.GetSchema(txtOrclSchemaName.Text); } else if (txtOrclSchemaName.Text.Length > 0) { schema = conn.GetSchema(txtOrclSchemaName.Text, restriction); } else schema = conn.GetSchema(); grdGrid.DataSource = schema; grdColumns.DataSource = GetColumns(schema.Columns); } } catch { throw; } }
public static void Main(string[] args) { var nameSpace = args[0]; var match = new Regex("^(?<user>[a-zA-Z0-9]+)/(?<password>[a-zA-Z0-9]+)@(?<tnsName>[a-zA-Z0-9]+)").Match(args[1]); var user = match.Groups["user"].Value; var password = match.Groups["password"].Value; var tnsName = match.Groups["tnsName"].Value; var connectionString = String.Format( "User Id={0};Password={1};Data Source={2}", user, password, tnsName); using (var conn = new OracleConnection(connectionString)) { try { conn.Open(); } catch (OracleException ex) { Console.WriteLine(ex.Message); return; } var typeDictionary = conn.GetSchema("DataTypes").AsDynamic() .ToDictionary(x => x.TypeName, x => (Type)Type.GetType(x.DataType)); Func<string, bool, decimal, decimal, string> getTypeName = (dataType, isNullable, precision, scale) => { var type = typeDictionary[dataType]; var typeName = type.Name; if (precision > 0 && scale == 0) { // NUMBER型が無条件にDecimalになるので、小数桁数が0なら整数型として扱う if (precision < 10) { typeName = "Int32"; } else { typeName = "Int64"; } } return (isNullable && type.IsValueType) ? typeName + "?" // 値型かつnull許可の時 : typeName; }; var columnGroups = conn.GetSchema("Columns").AsDynamic() .Where(x => x.OWNER == user) .GroupBy(x => x.TABLE_NAME) // 全てのカラムが平らに列挙されてくるのでテーブル名でグルーピング .Select(g => new { ClassName = g.Key, // クラス名はテーブル名(= グルーピングのキー) Properties = g .OrderBy(x => x.ID) // どんな順序で来るか不明なので、カラム定義順にきちんと並び替え .Select(x => new { Name = x.COLUMN_NAME, Type = getTypeName( x.DATATYPE, x.NULLABLE == "Y", x.PRECISION ?? 0m, x.SCALE ?? 0m) }) .ToArray() }); foreach (var item in columnGroups) { var tt = new TableGeneratorTemplate(nameSpace, item.ClassName, item.Properties); var text = tt.TransformText(); File.WriteAllText(item.ClassName + ".cs", text, Encoding.UTF8); } } }