如何从Sql表中检索DataColumn.DefaultValue? [英] How do I retrieve DataColumn.DefaultValue from a Sql Table?
问题描述
是否可以使用ado.net从Sql数据库中确定列的默认值?
Is there a way to determine a column's defaultvalue from the Sql Database using ado.net?
我尝试使用 SqlDataAdapter
的 FillSchema
方法:
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);
}
当我检查 DataColumns $ c $
DataTable
中的c>,我可以确定列是否是 AutoIncrement
,并可以确定它是否允许使用null AllowDBNull
属性。但是, DefaultValue
(对于我知道具有默认值的列)始终为 null
。
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
.
我认为:
DataTable schemaTable = null;
using (SqlDataReader reader = adapter.SelectCommand.ExecuteReader(CommandBehavior.SchemaOnly)) {
schemaTable = reader.GetSchemaTable();
reader.Close();
}
但 DefaultValue
是
所以...如何获取列的 DefaultValue
?
So...how can I get a column's DefaultValue
?
推荐答案
使用此查询对 INFORMATION_SCHEMA
询问您要查找的信息:
Use this query to interrogate the INFORMATION_SCHEMA
for the info you're looking for:
SELECT
TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'your table name' AND
COLUMN_NAME = 'your column name'
马克
这篇关于如何从Sql表中检索DataColumn.DefaultValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!