c# - 如何在 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;
}
解决方案
我建议声明一个像 blow 这样的变量,然后添加到它的列表中。
foreach (DataRow dr in dt.Rows)
{
var emp=new MyEmployee
{
CountId = Convert.ToInt32(dr["CountId"])
});
list.Add(emp);
}
推荐阅读
- python - 'TypeError:需要整数参数,得到浮点数'
- html - 如何去掉浏览器默认设置的省略号?
- node.js - 使用 Alexa Skills 节点 SDK 在 Echo Show 上显示全屏图像
- javascript - IE11表单提交事件被调用,但是表单没有提交
- ansible - 有条件的ansible任务
- python - 我们可以在 python 的 def __init__() 中定义一个函数吗
- python - 如何从python中的shell脚本中读取常量
- sql - 如何在 SQL 中将两个时间值相加
- powershell - 为什么powershell会改变变量数据?我可以让它停下来吗?
- image - 通过整个目录将 RGB 值重新排列为 GRB、GBR、BRG、BGR 和 RBG