问题描述
我有以下方法.在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;
}
推荐答案
这篇关于通用清单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!