通用清单问题 [英] Problem with Generic List

查看:76
本文介绍了通用清单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法.在While块中,我向列表中添加了不同的对象,但是当返回列表时,其所有元素都是相同的.能帮帮我吗?



公共静态列表< Bo_Session> ReadAll(DateTime日期)
{
SqlConnection连接= GetConnection();
字符串procedureName ="[dbo].[sp_UserSession_SelectAll]";
SqlCommand selectCommand =新的SqlCommand(procedureName,连接);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.AddWithValue("@ pI_Date",日期);
列表< Bo_Session> bo_Session_List =新列表< Bo_Session>();
试试
{
connection.Open();
SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.Default);

while(reader.Read())
{
Bo_Session locale_bo_Session =新Bo_Session();

locale_bo_Session.SessionId = System.Convert.ToInt32(reader ["SessionId"]是DBNull?0:reader ["SessionId"]);
locale_bo_Session.LoggedUserId = System.Convert.ToInt32(reader ["UserId"] is DBNull?0:reader ["UserId"]);
locale_bo_Session.LoggedUserName = System.Convert.ToString(reader ["UserName"]是DBNull吗?":reader ["UserName"]);
locale_bo_Session.ApplicationId = System.Convert.ToInt32(reader ["ApplicationId"] is DBNull?0:reader ["ApplicationId"]);
locale_bo_Session.ComputerName = System.Convert.ToString(reader ["ComputerName"]是DBNull吗?":reader ["ComputerName"]);
locale_bo_Session.SessionStart = System.Convert.ToDateTime(reader ["SessionStart"]是DBNull?System.DateTime.Now:reader ["SessionStart"]));
locale_bo_Session.SessionEnd = System.Convert.ToDateTime(reader ["SessionEnd"]是DBNull?System.DateTime.Now:reader ["SessionEnd"]));
locale_bo_Session.SessionLastRetrieve = System.Convert.ToDateTime(reader ["SessionLastRetrieve"]是DBNull?System.DateTime.Now:reader ["SessionLastRetrieve"]);
locale_bo_Session.SessionStatus = System.Convert.ToBoolean(reader ["SessionStatus"]是DBNull?0:reader ["SessionStatus"]);

bo_Session_List.Add(locale_bo_Session);

}
reader.Close();
}
捕获(SqlException ex)
{
扔前;
}
终于
{
connection.Close();
}
返回bo_Session_List;
}

I have the following method. In the While block i add different objects to the list, but when the list is returned all its elements are the same. Can help me?



public static List<Bo_Session> ReadAll(DateTime date)
{
SqlConnection connection = GetConnection();
string procedureName = "[dbo].[sp_UserSession_SelectAll]";
SqlCommand selectCommand = new SqlCommand(procedureName, connection);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.AddWithValue("@pI_Date", date);
List<Bo_Session> bo_Session_List = new List<Bo_Session>();
try
{
connection.Open();
SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.Default);

while (reader.Read())
{
Bo_Session locale_bo_Session = new Bo_Session();

locale_bo_Session.SessionId = System.Convert.ToInt32(reader["SessionId"] is DBNull ? 0 : reader["SessionId"]);
locale_bo_Session.LoggedUserId = System.Convert.ToInt32(reader["UserId"] is DBNull ? 0 : reader["UserId"]);
locale_bo_Session.LoggedUserName = System.Convert.ToString(reader["UserName"] is DBNull ? "" : reader["UserName"]);
locale_bo_Session.ApplicationId = System.Convert.ToInt32(reader["ApplicationId"] is DBNull ? 0 : reader["ApplicationId"]);
locale_bo_Session.ComputerName = System.Convert.ToString(reader["ComputerName"] is DBNull ? "" : reader["ComputerName"]);
locale_bo_Session.SessionStart = System.Convert.ToDateTime(reader["SessionStart"] is DBNull ? System.DateTime.Now : reader["SessionStart"]);
locale_bo_Session.SessionEnd = System.Convert.ToDateTime(reader["SessionEnd"] is DBNull ? System.DateTime.Now : reader["SessionEnd"]);
locale_bo_Session.SessionLastRetrieve = System.Convert.ToDateTime(reader["SessionLastRetrieve"] is DBNull ? System.DateTime.Now : reader["SessionLastRetrieve"]);
locale_bo_Session.SessionStatus = System.Convert.ToBoolean(reader["SessionStatus"] is DBNull ? 0 : reader["SessionStatus"]);

bo_Session_List.Add(locale_bo_Session);

}
reader.Close();
}
catch (SqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
return bo_Session_List;
}

推荐答案

在我看来,您是正确的,您在循环内创建了一个新对象,因此我看不出它失败的原因.您是否已遍历代码以查看正在发生什么情况?
This looks correct to me, you create a new object inside the loop, so I see no reason for it to fail. Have you stepped through the code to see what is happening ?


这篇关于通用清单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆