【问题标题】:Return values from SQL Server to C# convert ToArray?从 SQL Server 返回值到 C# 转换 ToArray?
【发布时间】:2014-12-21 00:22:21
【问题描述】:

我目前正在处理一项任务,试图从 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();
}

处得到错误
Bays.Add(temp)

【问题讨论】:

  • 欢迎来到 SO!请注意,在数据合约中进行数据访问很奇怪。数据契约用于接受或返回数据,通常不用于数据访问。
  • 您正在尝试构建一个车库列表,因此您应该给出车库对象的完整定义,不仅是它的构造函数,还应该给出数据库中数据表车库中的字段是什么?您需要一一阅读并将它们添加到您的车库模型属性中
  • reader.GetOrdinal("Bay") 将获取Bay 列的列序号(即数字) - 对于读取的每一行。请参阅 SqlDataReader.GetOrdinal 上的 MSDN 文档。
  • 您从查询中读取的temp 变量是int - 但是,BaysGarage 对象的列表。那些不兼容。您需要创建一个Garage 实例,从读取器中填充其值,然后将Garage 对象添加到Bays 列表中。
  • @christiandev Garage 只是一个 SQL 数据库,其中一列 int 表示托架编号

标签: c# sql-server list store toarray


【解决方案1】:

reader.Read() 的每次调用都会使读者前进到结果集的下一行。您似乎已经假设您可以通过一次读取将所有行都放入您的数组中。

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

我会输入代码,但我正在使用手机。 :)

【讨论】:

    【解决方案2】:

    将 Bays 更改为 List&lt;int&gt;,并返回一个列表或在列表上调用 .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")));
                    }         
                }
            } 
         }
         ..... 
    

    【讨论】:

      【解决方案3】:

      需要在您的代码中进行少量修改以获得您所需的结果。

       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();
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 2019-03-08
        相关资源
        最近更新 更多