【问题标题】:How do I put values from a column of a SQL table into an array in C#?如何将 SQL 表的列中的值放入 C# 中的数组中?
【发布时间】:2021-01-21 12:07:25
【问题描述】:

我想把 SQL 表的一列的值放入一个数组中。

SqlCommand cmd = new SqlCommand("SELECT ID FROM tblTray2");
string[] array = new string[ID];

这就是我所拥有的。

有人有提示吗?

【问题讨论】:

标签: c# sql arrays


【解决方案1】:

试试这个。

int i = 0;
SqlCommand cmd = new SqlCommand("SELECT ID FROM tblTray2");
SqlDataReader reader = cmd.ExecuteReader();
string[] arrayID = new string[ID];

while(reader.Read() == true)
{
    arrayID[i] = reader["ID"].ToString();
    i++;
}

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多