2

I want to get the default values of all columns of a table so I can display them in the data-entry form, is there a way to do this using ADO.NET ? I know I can query the information_schema database for this info, I'm just wondering if there's a built-in way to do this in ADO.NET

1
  • You can get almost everything else you'd want to know about the table schema by using FillSchema, but I don't see the default value among the available items - msdn.microsoft.com/en-us/library/229sz0y5.aspx Commented Sep 10, 2010 at 19:42

2 Answers 2

2

You can use DbConnection.GetSchema to get the default column values:

DbConnection conn = SqlClientFactory.Instance.CreateConnection("server=.\\SQLEXPRESS;database=northwind;integrated security=true");
conn.Open();

DataTable schema;

try {

   schema = conn.GetSchema("Columns", new string[4] { conn.Database, null, "products", null });

   foreach (DataRow row in schema.Rows) {
      Console.WriteLine("Name: {0}, Default: {1}", row["COLUMN_NAME"], row["COLUMN_DEFAULT"]);
   }

} finally {
   if (conn.State != ConnectionState.Closed)
      conn.Close();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Nope, you'll have to query the database schema, or hard code the values yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.