如何选择MySQL列标头,并在asp.net的gridview列中显示它们

本文关键字:net asp gridview 显示 并在 何选择 选择 MySQL | 更新日期: 2023-09-27 18:10:34

我想选择mysql列名和值,并在gridview表中为每个值显示它们。这是一个插图

+------+-------+------+-------+-------+
| id   |amt1   |amt2  |amt3   |amt4   |
+------+-------+------+-------+-------+
|  1   | 300   | 500  | 460   | 780   |
+------+-------+------+-------+-------+
|  2   | 450   | 230  | 320   | 410   |
+------+-------+------+-------+-------+
|  3   | 350   | 540  | 430   | 310   |
+------+-------+------+-------+-------+

我希望这个mysql表显示在gridview表

+-------------+--------+
|particulars  | amount |
+-------------+--------+
|  amt1       | 300    |
+-------------+--------+
|  amt2       | 500    |
+-------------+--------+
|  amt3       | 460    |
+-------------+--------+
|  amt4       | 780    |
+-------------+--------+

如何选择MySQL列标头,并在asp.net的gridview列中显示它们

只需运行其中的两个查询,第一个将为您提供列名,第二个将为您提供您正在查找的特定id的行。获得这些数据后,使用System.Data.SqlClient.SqlConnection从这些数据创建表应该非常简单。

columnNamesQuery = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'YourTableName'";
dataQuery = "SELECT * FROM YourTableName WHERE id = 1";

select 'amt1' particulars, amt1 amount from t1 where id=1 
union 
select 'amt2' particulars, amt2 amount from t1 where id=1 
union 
select 'amt3' particulars, amt3 amount from t1 where id=1
union 
select 'amt4' particulars, amt4 amount from t1 where id=1