我的网站(远程)上有一个sql server表。该表名为table1
,包含一堆字段。我的目标是将table1
的所有字段读入数组以进行迭代。
这是我的尝试:
private static void ShowFields()
{
using (SqlConnection connection = new SqlConnection(connectionstring))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table1'", connection);
SqlDataReader reader = command.ExecuteReader();
//connection.Close();
int colCount = reader.FieldCount;
while (reader.Read())
{
for (int i = 0; i < colCount; i++)
{
Console.WriteLine(reader[i]);
}
}
}
}
这几乎可以工作,但它显示了表的所有属性,而不是字段中的数据 - 例如,varchar,50 dao,table等。
答案 0 :(得分:1)
如果我理解正确你想要表格中的实际数据,不过你要查询的是INFORMATION_SCHEMA,它会为你提供有关表格/栏目/数据的数据......
所以只需查询表格如下:
SELECT * FROM table1
我不知道您的表中的列名,但是如果您只想显示一些列,则可以用列列表替换*:
SELECT col1, col2, col3 FROM table1
其中col1,col2和col3只是列的名称。
这是你想要的,还是我在这里的标记?