Find the number of columns in a table

前端 未结 19 2257
Happy的楠姐
Happy的楠姐 2020-11-27 11:13

It is possible to find the number of rows in a table:

select count(*) from tablename

Is it possible to find the number of columns in a tabl

19条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 11:58

    A MySQL answer adapted slightly from the MSDN example for MySqlDataReader.GetValues:

    //assumes you've already created a connection, opened it, 
    //and executed a query to a reader
    
    while(reader.Read())
    {
        Object[] values = new Object[reader.FieldCount];
        int fieldCount = reader.GetValues(values);
    
        Console.WriteLine("\nreader.GetValues retrieved {0} columns.",   fieldCount);
        for (int i = 0; i < fieldCount; i++)
            Console.WriteLine(values[i]);
    }
    

    Using MySqlDataReader.FieldCount will allow you to retrieve the number of columns in the row you've queried.

提交回复
热议问题