我在 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; }
1 回答

收到一只叮咚
TA貢獻1821條經驗 獲得超5個贊
我建議聲明一個像打擊這樣的變量然后添加到它的列表中。
foreach (DataRow dr in dt.Rows)
{
var emp=new MyEmployee
{
CountId = Convert.ToInt32(dr["CountId"])
});
list.Add(emp);
}
- 1 回答
- 0 關注
- 183 瀏覽
添加回答
舉報
0/150
提交
取消