Query vs. C# method to retrieve database metadata

天大地大妈咪最大 提交于 2019-12-24 11:27:07

问题


I need to retrieve for each table in the database the following info:

  1. All columns names
  2. For each column its type
  3. Type max length

The possible way to do that is to run a query (even can execute it using await, i.e. async):

select object_NAME(c.object_id), c.name, t.name, c.max_length
from sys.columns c
INNER JOIN sys.types t
    ON t.system_type_id = c.system_type_id

On the other hand there is GetSchema method on connection which makes the same:

DataTable columns = connection.GetSchema(SqlClientMetaDataCollectionNames.Columns, restrictions);
foreach (DataRow row in columns.Rows)
{
    string columnName = row[3].ToString();
    string columnDataType = row[7].ToString();   
    string columnDataTypeLen = row[8].ToString();
}

Which of the methods is better to use? Looks that second one should be faster - am I right? What about performance?


回答1:


Use either method and cache the result

Meta data doesn't change under normal operation so performance is a non-issue



来源:https://stackoverflow.com/questions/22053829/query-vs-c-sharp-method-to-retrieve-database-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!