我在我的网站上有一个sql服务器表(远程)。这个表名为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等。
http://i.imgur.com/2bsgMBC.png
发布于 2013-05-20 08:27:27
如果我理解正确的话,您想要表中的实际数据,但是您查询的是INFORMATION_SCHEMA,它给出了表/列/其他内容的数据……
因此,只需像这样查询表:
SELECT * FROM table1我不知道您的表中的列名,但如果您只想显示一些列,您可以用列列表替换*:
SELECT col1, col2, col3 FROM table1其中col1、col2和col3只是列的名称。
这就是你想要的,还是我说得太离谱了?
https://stackoverflow.com/questions/16640850
复制相似问题