Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. public IEnumerable<DbColumnInfo> GetColumns(string providerName, string connectionString, string tableName)
  2. {
  3. DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
  4. using (DbConnection connection = factory.CreateConnection())
  5. using (DbCommand command = factory.CreateCommand())
  6. {
  7. connection.ConnectionString = connectionString;
  8. connection.Open();
  9. command.Connection = connection;
  10.  
  11. var columns = connection.GetSchema("Columns", tableName.Split('.'));
  12. foreach (DataRow row in columns.Rows)
  13. {
  14. yield return new DbColumnInfo()
  15. {
  16. Name = row.Field<string>(3),
  17. OrdinalPosition = row.Field<short>(4),
  18. DataType = this.FormatDataType(row),
  19. IsNullable = string.Equals(row.Field<string>(6), "yes", StringComparison.InvariantCultureIgnoreCase),
  20. IsPrimaryKey = // ... ?
  21. };
  22. }
  23. }
  24. }
  25.  
  26. var da = factory.CreateDataAdapter();
  27. command.CommandText = "select * from Employees";
  28. da.SelectCommand = command;
  29. da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  30.  
  31. var dtab = new DataTable();
  32. da.FillSchema(dtab, SchemaType.Source);
  33.  
  34. foreach (DataColumn col in dtab.Columns)
  35. {
  36. string name = col.ColumnName;
  37. bool isNull = col.AllowDBNull;
  38. bool isPrimary = dtab.PrimaryKey.Contains(col);
  39. }
  40.  
  41. public IEnumerable<DbColumnInfo> GetColumns(string connectionString, DbTableInfo table)
  42. {
  43. DbProviderFactory factory = DbProviderFactories.GetFactory(this.providerName);
  44. using (DbConnection connection = factory.CreateConnection())
  45. using (DbCommand command = factory.CreateCommand())
  46. {
  47. connection.ConnectionString = connectionString;
  48. connection.Open();
  49. command.Connection = connection;
  50. command.CommandText = ColumnInfoQuery;
  51. command.CommandType = CommandType.Text;
  52. var tableSchema = factory.CreateParameter();
  53. tableSchema.ParameterName = "@tableSchema";
  54. tableSchema.DbType = DbType.String;
  55. tableSchema.Value = table.Schema;
  56. command.Parameters.Add(tableSchema);
  57. var tableName = factory.CreateParameter();
  58. tableName.ParameterName = "@tableName";
  59. tableName.DbType = DbType.String;
  60. tableName.Value = table.Name;
  61. command.Parameters.Add(tableName);
  62.  
  63. var dataTable = new DataTable();
  64. using (var reader = command.ExecuteReader())
  65. {
  66. while (reader.Read())
  67. {
  68. yield return new DbColumnInfo()
  69. {
  70. Name = reader.GetString(0),
  71. OrdinalPosition = reader.GetInt32(1),
  72. DataType = reader.GetString(2),
  73. IsNullable = reader.GetBoolean(3),
  74. IsPrimaryKey = reader.GetBoolean(4),
  75. IsForeignKey = reader.GetBoolean(5),
  76. IsUnique = reader.GetBoolean(6),
  77. };
  78. }
  79. }
  80. }
  81. }
  82.  
  83. string[] restrictions = new string[] { null, null, strTable };
  84. DataTable tableInfo = Connection.GetSchema("IndexColumns", restrictions);
  85.  
  86. if (tableInfo == null)
  87. throw new Exception("TableInfo null Error");
  88.  
  89. foreach (DataRow test in tableInfo.Rows)
  90. {
  91. Console.WriteLine(test["column_name"]);
  92. }
  93.  
  94. DataTable indexes = conn.GetSchema("Indexes");
  95. List<string> PrimaryKeys = new List<string>();
  96. foreach (DataRow row in indexes.Rows)
  97. if (Convert.ToBoolean(row["PRIMARY_KEY"]))
  98. PrimaryKeys.Add(row["TABLE_NAME"] + "." + row["COLUMN_NAME"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement