Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IEnumerable<DbColumnInfo> GetColumns(string providerName, string connectionString, string tableName)
- {
- DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
- using (DbConnection connection = factory.CreateConnection())
- using (DbCommand command = factory.CreateCommand())
- {
- connection.ConnectionString = connectionString;
- connection.Open();
- command.Connection = connection;
- var columns = connection.GetSchema("Columns", tableName.Split('.'));
- foreach (DataRow row in columns.Rows)
- {
- yield return new DbColumnInfo()
- {
- Name = row.Field<string>(3),
- OrdinalPosition = row.Field<short>(4),
- DataType = this.FormatDataType(row),
- IsNullable = string.Equals(row.Field<string>(6), "yes", StringComparison.InvariantCultureIgnoreCase),
- IsPrimaryKey = // ... ?
- };
- }
- }
- }
- var da = factory.CreateDataAdapter();
- command.CommandText = "select * from Employees";
- da.SelectCommand = command;
- da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
- var dtab = new DataTable();
- da.FillSchema(dtab, SchemaType.Source);
- foreach (DataColumn col in dtab.Columns)
- {
- string name = col.ColumnName;
- bool isNull = col.AllowDBNull;
- bool isPrimary = dtab.PrimaryKey.Contains(col);
- }
- public IEnumerable<DbColumnInfo> GetColumns(string connectionString, DbTableInfo table)
- {
- DbProviderFactory factory = DbProviderFactories.GetFactory(this.providerName);
- using (DbConnection connection = factory.CreateConnection())
- using (DbCommand command = factory.CreateCommand())
- {
- connection.ConnectionString = connectionString;
- connection.Open();
- command.Connection = connection;
- command.CommandText = ColumnInfoQuery;
- command.CommandType = CommandType.Text;
- var tableSchema = factory.CreateParameter();
- tableSchema.ParameterName = "@tableSchema";
- tableSchema.DbType = DbType.String;
- tableSchema.Value = table.Schema;
- command.Parameters.Add(tableSchema);
- var tableName = factory.CreateParameter();
- tableName.ParameterName = "@tableName";
- tableName.DbType = DbType.String;
- tableName.Value = table.Name;
- command.Parameters.Add(tableName);
- var dataTable = new DataTable();
- using (var reader = command.ExecuteReader())
- {
- while (reader.Read())
- {
- yield return new DbColumnInfo()
- {
- Name = reader.GetString(0),
- OrdinalPosition = reader.GetInt32(1),
- DataType = reader.GetString(2),
- IsNullable = reader.GetBoolean(3),
- IsPrimaryKey = reader.GetBoolean(4),
- IsForeignKey = reader.GetBoolean(5),
- IsUnique = reader.GetBoolean(6),
- };
- }
- }
- }
- }
- string[] restrictions = new string[] { null, null, strTable };
- DataTable tableInfo = Connection.GetSchema("IndexColumns", restrictions);
- if (tableInfo == null)
- throw new Exception("TableInfo null Error");
- foreach (DataRow test in tableInfo.Rows)
- {
- Console.WriteLine(test["column_name"]);
- }
- DataTable indexes = conn.GetSchema("Indexes");
- List<string> PrimaryKeys = new List<string>();
- foreach (DataRow row in indexes.Rows)
- if (Convert.ToBoolean(row["PRIMARY_KEY"]))
- PrimaryKeys.Add(row["TABLE_NAME"] + "." + row["COLUMN_NAME"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement