Is there a way to determine the default value of a column from an Sql database using ado.net?
I tried using the SqlDataAdapter 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 check the DataColumns in a DataTable , I can determine if the column is AutoIncrement , and can determine if it allows null using the AllowDBNull property. However, DefaultValue (for columns that I know have a default value) is always null .
I thought:
DataTable schemaTable = null; using (SqlDataReader reader = adapter.SelectCommand.ExecuteReader(CommandBehavior.SchemaOnly)) { schemaTable = reader.GetSchemaTable(); reader.Close(); }
but DefaultValue not included in the schema.
So ... how can I get the DefaultValue column?
source share