Is there a way to determine a column's defaultvalue from the Sql Database using ado.net?
I tried using the SqlDataAdapter
's FillSchema
method:
using (SqlDataAdapter adapter = new SqlDataAdapter()) {
adapter.SelectCommand = myConnection.CreateCommand();
adapter.SelectCommand.CommandType = CommandType.Text;
adapter.SelectCommand.CommandText = "SELECT * FROM myTable";
DataTable table = new DataTable();
adapter.Fill(table);
adapter.FillSchema(table, SchemaType.Mapped);
}
When I inspect the DataColumns
in the DataTable
, I can determine if a column is an AutoIncrement
, and can determine if it allows nulls using the AllowDBNull
property. However, DefaultValue
(for columns that I know have a default value) is always null
.
I considered:
DataTable schemaTable = null;
using (SqlDataReader reader = adapter.SelectCommand.ExecuteReader(CommandBehavior.SchemaOnly)) {
schemaTable = reader.GetSchemaTable();
reader.Close();
}
but DefaultValue
is not included in the schema.
So...how can I get a column's DefaultValue
?