【问题标题】:How to populate collection with values from dataset如何使用数据集中的值填充集合
【发布时间】:2013-01-12 00:35:36
【问题描述】:

这是一个新手问题,我正在尝试用 C# 构建一个类,该类将为用户设置 UserOrgs 属性(每个用户可以拥有多个)

到目前为止我有这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for clsRepUser
/// </summary>
public class clsUser
{
    private string userid;
    private List<string> userorgs;

    public string UserID
    {
        get
        {
            return userid;
        }
        set
        {
            userid = value;
        }
    }

    public List<string> UserOrgs
    {
        get
        {
            return userorgs;
        }
        set
        {
            userorgs = value;
        }
    }

    clsConn cCon = new clsConn();
    String connStr = "";
    public clsUser()
    {

    }

    public DataSet GetUserOrg(string UserID)
    {
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(cCon.getConn());

        SqlCommand cmd = new SqlCommand("sp_getUserOrgs", conn);

        // 2. set the command object so it knows
        // to execute a stored procedure
        cmd.CommandType = CommandType.StoredProcedure;

        // 3. add parameter to command, which
        // will be passed to the stored procedure
        cmd.Parameters.Add(
            new SqlParameter("@UserID", UserID));

        try 
        {
            // Open the connection and execute the Command
            conn.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();

            sqlDA.SelectCommand = cmd;
            sqlDA.Fill(ds);
        } 
        catch (Exception ex) 
        {
        } 
        finally 
        {
            conn.Close();
        }

        return ds;    
    }
}

我现在如何从GetUserOrg 函数填充该用户的UserOrgs 属性?还是我在这方面做得不够好?

【问题讨论】:

  • 这是非常糟糕的代码:catch (Exception ex) {} 这就像断开孩子卧室的烟雾探测器。
  • @SteveWellens 我不确定我是否会将在家庭作业中忽略数据库错误等同于危及孩子的生命......
  • @itsme86 如果它是用于诊断您孩子疾病的医学数据库,您会这样做。我用夸张的手法来说明问题。让我们尽量保持话题。
  • Reading DataSet 的可能重复项

标签: c# .net datatable dataset


【解决方案1】:

您正在使用存储过程,因此很难为您提供准确的代码,但我们的想法是遍历 DataSet 的所有行,并使用 .Add() 方法将该行的元素添加到列表中。像这样的:

foreach (DataRow dr in ds.Tables[0].Rows) {  
  userorgs.Add(dr["orgs_column"].ToString()); 
}

【讨论】:

  • Bee ds 是数据集,您可以添加 Tables 属性以访问表属性,并获取行数
  • 我很乐意帮助你蜜蜂
【解决方案2】:

我会使用 orm,linqtosql 对新手来说很容易。如果您坚持使用数据表和数据阅读器

您需要遍历返回的结果并将返回值填充到您的对象中。

This is a pretty good primer to start of with.

【讨论】:

    【解决方案3】:

    我认为有3种方式:

    1. 您可以使用通用Lazy&lt;&gt;
    2. 将属性替换为方法GetUserOrganizations()。它更清楚。如果您想获得“新鲜”的组织。
    3. 将获取逻辑移至另一层(BI、DAL)。那么用户类型就像数据合约。

    【讨论】:

      【解决方案4】:

      您可以尝试使用此代码 - 基于 DataSet.Tables[0].Rows

      using(var conn = new SqlConnection(cCon.getConn())
      {
              using(var cmd = new SqlCommand("sp_getUserOrgs", conn))
              {
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.Add(new SqlParameter("@UserID", UserID));
      
                try 
                {
                  conn.Open();
                  SqlDataAdapter sqlDA = new SqlDataAdapter();
      
                  sqlDA.SelectCommand = cmd;
                  sqlDA.Fill(ds);
      
                  foreach (DataRow dr in ds.Tables[0].Rows) 
                  {  
                     userorgs.Add(dr["orgs_column"].ToString()); 
                  }
                } 
                catch (Exception ex) 
                {
                  //treat your exception
                } 
            }       
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-09
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 2016-09-17
        • 1970-01-01
        • 2016-01-30
        • 2010-11-27
        相关资源
        最近更新 更多