首页 > 解决方案 > 如何在 C# 函数中返回 ADO.NET 中的计数值?

问题描述

我在 C# 中创建了 ADO.NET,我想在其中返回计数值。我的程序GetAllEmployees @UserId返回员工总数。但list总是返回1,即使CountId显示0。我正在做一些愚蠢的事情。谁能帮帮我吗。

  public List<MyEmployee> GetAll(int UserId)
    {
        clsClass cls = new clsClass();
        DataSet ds;
        List<MyEmployee> list = new List<MyEmployee>();

        SqlParameter[] prms = new SqlParameter[1];
        string sSQL;
        sSQL = "exec GetAllEmployees @UserId";
        prms[0] = new SqlParameter("@UserId", SqlDbType.Int);
        prms[0].Value = UserId;
        ds = cls.CommandwithParams(sSQL, prms);
        DataTable dt = ds.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(
                new MyEmployee
                {
                    CountId = Convert.ToInt32(dr["CountId"])

                });
        }
        return list;
    }

标签: c#ado.net

解决方案


我建议声明一个像 blow 这样的变量,然后添加到它的列表中。

foreach (DataRow dr in dt.Rows)
 {
          var emp=new MyEmployee
            {
                CountId = Convert.ToInt32(dr["CountId"])
            });  
          list.Add(emp);
 }

推荐阅读