簡體   English   中英

從存儲過程返回數據的對象返回列表C#

[英]Return List of Objects from stored procedure return data C#

我有一個方法,它調用存儲過程並返回對象列表。 我不確定如何將存儲過程中的結果添加到對象列表中。 我嘗試使用模型。 添加,但無論如何我正在使用它,我得到了錯誤。 我在代碼中確定了需要幫助的地方。

這是我的代碼:

    public List<Models.Type> get_Type(string Type_Group)
    {
        string connectionName = getConnectionStr();
        List<Models.Type> model = null;

        string myConnection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString();
        SqlDatabase db = new SqlDatabase(myConnection);

        using (DbCommand command = db.GetStoredProcCommand("LA_Get_Type"))
        {
            db.AddInParameter(command, "Type_Group", DbType.String, Type_Group);

            var result = db.ExecuteReader(command);

            try
            {
                if (result.FieldCount == 0)
                    model = null;
                else
                {
                    while (result.Read())
                    {
                     model = new List<Models.Type>()
                        {

    //This is the place I don't know I tried   model.Add but not sure what 
     to have after. 
     This code is when I have returning just 1 object but I want to 
     return list of objects                           


                            typeID = Convert.ToInt32(result["typeID"].ToString()),
                            type_group = result["type_group"].ToString(),
                            type_value = result["type_value"].ToString(),
                            type_desc = result["type_desc"].ToString(),
                            type_sort = Convert.ToInt32(result["type_sort"].ToString())
                        };
                    }
                }
            }
            catch (Exception ex)
            {
            }
            result.Close();
            return model;
        }
    }

這是我的目標:

  public class Type
  {

    public int typeID { get; set; }
    public string type_group { get; set; }
    public string type_value { get; set; }
    public string type_desc { get; set; }
    public int type_sort { get; set; }

  }

更改

                while (result.Read())
                {
                 model = new List<Models.Type>()
                    {

//This is the place I don't know I tried   model.Add but not sure what 
 to have after. 
 This code is when I have returning just 1 object but I want to 
 return list of objects                           


                        typeID = Convert.ToInt32(result["typeID"].ToString()),
                        type_group = result["type_group"].ToString(),
                        type_value = result["type_value"].ToString(),
                        type_desc = result["type_desc"].ToString(),
                        type_sort = Convert.ToInt32(result["type_sort"].ToString())


                    };
                }

像這樣:

                model = new List<Models.Type>();
                while (result.Read())
                {
                    Models.Type aModel = new Model(){
                        typeID = Convert.ToInt32(result["typeID"].ToString()),
                        type_group = result["type_group"].ToString(),
                        type_value = result["type_value"].ToString(),
                        type_desc = result["type_desc"].ToString(),
                        type_sort = Convert.ToInt32(result["type_sort"].ToString())
                    };
                    model.Add(aModel);
                }

請注意,我正在為每個結果創建一個新對象,然后將它們一個一個地添加到列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM