C# 从SQL Server返回值到C转换为Array?

C# 从SQL Server返回值到C转换为Array?,c#,sql-server,list,store,toarray,C#,Sql Server,List,Store,Toarray,我目前正在处理一项任务,试图从SQL Server数据库中获取值并将其存储在数组中。我的连接正常,但将返回值放入数组时遇到问题 这是我得到的,自从我问了这个问题后,我做了一些改变: public int Bay; int temp; [DataContract] public Garage() { List<Garage> Bays = new List<Garage>(); SqlConnection connection = new SqlConn

我目前正在处理一项任务,试图从SQL Server数据库中获取值并将其存储在数组中。我的连接正常,但将返回值放入数组时遇到问题

这是我得到的,自从我问了这个问题后,我做了一些改变:

public int Bay;
int temp;

[DataContract]
public Garage()
{
    List<Garage> Bays = new List<Garage>();

    SqlConnection connection = new SqlConnection("Data   Source=fastapps04.qut.edu.au;Initial Catalog=*******;User ID=******;Password=******");
    connection.Open();

    SqlCommand command = new SqlCommand("SELECT Bay FROM Garage", connection);

    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        temp = reader.GetOrdinal("Bay");
        Bays.Add(temp);
    }

    Bays.ToArray();

    reader.Close();
    connection.Close();
}
每次调用reader.Read都会将读取器提前到结果集的下一行。您似乎假设可以通过一次读取将所有行放入数组中

如果您更改代码,以便在while循环的每次迭代中将temp添加到数组中,您应该会发现它是有效的


我想输入密码,但我在打电话

为了获得所需的结果,只需对代码进行少量修改

 public Garage()
{

    SqlConnection connection = new SqlConnection("Data   Source=fastapps04.qut.edu.au;Initial Catalog=*******;User ID=******;Password=******");
    connection.Open();

    SqlCommand command = new SqlCommand("SELECT Bay FROM Garage", connection);

    SqlDataReader reader = command.ExecuteReader();
    List<Garage> listBays =new List<Garage>();
    while (reader.Read())
    {
        temp = reader.GetInt32(reader.GetOrdinal("Bay"));
        listBays.Add(temp);
    }
    Garage[] Bays=listBays.ToArray();
    reader.Close();
    connection.Close();

}
将间隔更改为列表,然后返回列表或列表上的call.ToArray。另外,将Using语句与连接和命令一起使用

   List<int> Bays = new List<int>(); 

   using(SqlConnection connection = new SqlConnection("connString"))
    {
        connection.Open();

        using (SqlCommand command = new SqlCommand("SELECT Bay FROM Garage", 
                                                                       connection))
        {
            using (SqlDataReader reader= command.ExecuteReader())
            {
                while (reader.Read()) 
                {
                    Bays.Add(reader.GetInt32(reader.GetOrdinal("Bay")));
                }         
            }
        } 
     }
     ..... 

欢迎来到SO!请注意,在数据契约中进行数据访问是很奇怪的。数据协定用于接受或返回数据,通常不用于数据访问。您正在尝试构建车库列表,因此您应该给出车库对象的完整定义,不仅包括其构造函数,还包括数据库中datatable Garage中的哪些字段?。你需要一个接一个地阅读这些字段,并将它们添加到你的车库模型属性Reader.GetOrdinalBay只需获取列序号,即每读取一行的间隔列编号。请参阅上的MSDN文档。从查询中读取的temp变量是一个int-但是,间隔是车库对象的列表。这些不兼容。您需要创建一个Garage实例,从读取器中填充其值,然后将Garage对象添加到间隔列表中。@christiandev Garage只是一个SQL数据库,其中有一列int表示间隔编号
   List<int> Bays = new List<int>(); 

   using(SqlConnection connection = new SqlConnection("connString"))
    {
        connection.Open();

        using (SqlCommand command = new SqlCommand("SELECT Bay FROM Garage", 
                                                                       connection))
        {
            using (SqlDataReader reader= command.ExecuteReader())
            {
                while (reader.Read()) 
                {
                    Bays.Add(reader.GetInt32(reader.GetOrdinal("Bay")));
                }         
            }
        } 
     }
     .....