如何将数据存储到List <string []>()的变量或属性中?

时间:2017-03-09 06:49:11

标签: c# sql-server

我编写了一个代码,用SP从SQL Server检索数据。但我不知道如何存储变量或属性。 SQL Scripts正在使用SQL Server的PIVOT函数将行转换为列。

示例代码:

 public List<string[]> GetAllFlatType(long? id)
        {
            var list = new List<string[]>();

            using (
                var cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)
            )
            {
                cn.Open();
                var cmd = new SqlCommand("SP_Calc_By_Flat_Type_with_Total", cn)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@LandID", id);

                using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            var rows = new string[rdr.FieldCount];

                            for (var i = 0; i < rdr.FieldCount; i++)
                                rows[i] = rdr[i] == DBNull.Value ? string.Empty : rdr[i].ToString();
                            list.Add(rows);
                        }
                        rdr.NextResult();
                    }
                    rdr.Close();
                }
                cn.Close();
            }

            return list;
        }

数据:

{
  "data": [
    [
      "1",
      "Total Transaction Amount",
      "520400958",
      "12210558",
      "349786707",
      "124624122",
      "33779571"
    ],
    [
      "1",
      "No. of units",
      "109",
      "3",
      "76",
      "25",
      "5"
    ],
    [
      "1",
      "(Total sq.ft.)",
      "46800",
      "900",
      "30400",
      "12500",
      "3000"
    ],
    [
      "1",
      "Avg. price",
      "4774320",
      "4070186",
      "4602456",
      "4984964",
      "6755914"
    ],
    [
      "1",
      "Avg. sq.ft.",
      "11119",
      "13567",
      "11506",
      "9969",
      "11259"
    ],
    [
      "1",
      "Max. price",
      "7947521",
      "4418469",
      "7947521",
      "7465860",
      "7293834"
    ],
    [
      "1",
      "Max. sq.ft",
      "19868",
      "14728",
      "19868",
      "14931",
      "12156"
    ],
    [
      "1",
      "Min. price",
      "3241412",
      "3857581",
      "3241412",
      "3566034",
      "6162669"
    ],
    [
      "1",
      "Min. sq.ft",
      "7132",
      "12858",
      "8103",
      "7132",
      "10271"
    ]
  ]
}

enter image description here

2 个答案:

答案 0 :(得分:0)

我会以下列方式推荐你。创建一个代表DataStructure的类:

//You can find a better name for Data
public class Data
{
   public int LandId{get;set;}
   public string Type{get;set;}
   public int Total{get;set;}
   public int Studio{get;set;}
   public int Bedroom1 {get;set;}
   public int Bedroom2{get;set;}
   public int BedroomAbc{get;set;}
}

然后,您需要将数据转换为此Dataclass中的对象:

//Hint: You don't need to call Close() if you use a using-block. It does the same ;)
//Hint2: One while is enough because Read() automatically goes to next result
using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
   while (rdr.Read())
   {
     //Here we create a new object
     var data = new Data();

     //Here we fill the object
     data.LandId = rdr.GetInt(0);
     data.Type = rdr.GetString(1);
     data.Total = rdr.GetInt(2);
     data.Studio = rdr.GetInt(3);
     data.Bedroom1 = rdr.GetInt(4);
     data.Bedroom2 = rdr.GetInt(5);
     data.BedroomAbc = rdr.GetInt(6);

     list.Add(data);
   }
}

确保您的方法不再返回List<string>,您需要返回List<Data>

您可以通过以下方式访问此数据:

List<Data> mydata = GetAllFlatType(5);
int dummy = mydata[0].LandId;

//OR
var data = mydata.Find(d => d.LandId == 5); //Find the object with LandId == 5
int dummy = data.Total;

如果将List设置为GridView的DataSource,它将自动像魅力一样工作:)

答案 1 :(得分:0)

您可以从阅读器中获取名称

for (var i = 0; i < rdr.FieldCount; i++)
    rows[i] = rdr[i] == DBNull.Value ? string.Empty : $"{rdr.GetName(i)}:{rdr[i].ToString()}";