简体   繁体   English

仅获取列名列表

[英]Get a list of column names only

I've been looking around the Internet for quite some time now, but I can't find a way to get a list of just column names. 我已经在Internet上浏览了很长时间,但是我找不到找到仅列名列表的方法。 I don't care about the data each column has. 我不在乎每列的数据。

I want to take that list and compare it against a collection. 我要列出该清单,并将其与收藏夹进行比较。 I'm using the VB.NET MySqlConnector. 我正在使用VB.NET MySqlConnector。 I'm new to using the connector. 我是使用连接器的新手。

Edit: 编辑:

Dim mscCMD As MySqlCommand = New MySqlCommand("SHOW COLUMNS FROM OCN.cpu", msc)
            Dim sqlReader As MySqlDataReader = mscCMD.ExecuteReader()
            Dim b As Integer = 0
            Dim c As Integer = 0
            While sqlReader.Read
                msProjects(c) = sqlReader.**Item**(b)
                b += 1
                c += 1
            End While

Never mind, I figured it out. 没关系,我知道了。 I had to choose Item, not getName. 我必须选择Item,而不是getName。

Edit: Perhaps I didn't just yet. 编辑:也许我还没有。 It's reading one row. 它正在读一行。 I'm unsure how to move to the next row. 我不确定如何移动到下一行。 For example 例如

mysql> SHOW COLUMNS FROM City;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
**Id         | int(11)  | NO   | PRI | NULL    | auto_increment**
| Name       | char(35) | NO   |     |         |                |
| Country    | char(3)  | NO   | UNI |         |                |
| District   | char(20) | YES  | MUL |         |                |
| Population | int(11)  | NO   |     | 0       |                |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

That's the row it reads it reads. 那就是它读取的行。 Once you go past the 5th item, you're out of bounds. 一旦超过了第五项,您就越界。

You can use the metadata from a result set: 您可以使用结果集中的元数据:

         final ResultSet rs = statement.executeQuery("SELECT * FROM `" + tableName +"` LIMIT 0,1;");
         final ResultSetMetaData metaData = rs.getMetaData();
         for(int i=1; i<metaData.getColumnCount(); i++)
         {
             System.out.println("" + metaData.getColumnName(i));
         }

Also, you can get the java.sql.Types column type the same way. 另外,您可以以相同的方式获取java.sql.Types列类型。

Got it. 得到它了。 Found the answer from a MS KB article. 从MS KB文章中找到答案。

https://support.microsoft.com/en-us/kb/310108 https://support.microsoft.com/en-us/kb/310108

For Each myField In schemaTable.Rows
    'For each property of the field...
     For Each myProperty In schemaTable.Columns
        'Display the field name and value.
         Console.WriteLine(myProperty.ColumnName & " = " & myField(myProperty).ToString())
     Next
     Console.WriteLine()

     'Pause.
     Console.ReadLine()
Next

Specifically myField(myProperty).ToString will get you the column names. 特别是myField(myProperty).ToString将为您获取列名。 It will also contain other things about the column. 它还将包含有关该列的其他内容。 You will have to sort through it, but all the column names from your table will be in there. 您将不得不对其进行排序,但是表中的所有列名称都将位于其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM