如何设置DataTable的特定顺序?

时间:2016-01-15 18:32:54

标签: vb.net

我通过查询填充DataTable,此查询获取我的数据库中可用的所有表名。现在我想要做的是将此表名称命名为特定索引,例如,这是返回的表:

appointments
planning
resources
service_categories
services
services_providers
settings
user_settings
users

我正在尝试做的是设置此顺序:

appointments
planning
resources
service_categories
services
settings
users
user_settings
services_providers

查询

query = "SELECT TABLE_NAME 
                        FROM INFORMATION_SCHEMA.TABLES
                        WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='"& My.Settings.Dbname & "' "

我希望在下一次迭代这个对象时,以这种特定格式订购DataTable'项的原因,并且我想要一个表名的精确迭代顺序。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

你可以试试这个,这是一种肮脏的方式,但它在我的结束。上面还提到了另外一种方式...

 SELECT TABLE_NAME
 FROM INFORMATION_SCHEMA.TABLES
 WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = "DBNAME"
 ORDER BY 
  CASE TABLE_NAME
    WHEN 'appointments' THEN 1
    WHEN 'planning' THEN 2
    WHEN 'resources' THEN 3
    WHEN 'service_categories' THEN 4
    WHEN 'services' THEN 5
    WHEN 'settings' THEN 6
    WHEN 'users' THEN 7
    WHEN 'user_settings' THEN 8
    WHEN 'services_providers' THEN 9
 END

以下是此

的字符串表示形式
 "SELECT TABLE_NAME
 FROM INFORMATION_SCHEMA.TABLES
 WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '" & My.Settings.Dbname & "'
 ORDER BY 
  CASE TABLE_NAME
    WHEN 'appointments' THEN 1
    WHEN 'planning' THEN 2
    WHEN 'resources' THEN 3
    WHEN 'service_categories' THEN 4
    WHEN 'services' THEN 5
    WHEN 'settings' THEN 6
    WHEN 'users' THEN 7
    WHEN 'user_settings' THEN 8
    WHEN 'services_providers' THEN 9
 END "