Login Login
MORE

WIDGETS

Widgets

Wanted articles
Who is online?
Article tools

CSharp:Ottimizzazione Esempio usando DataReader

From Aino Wiki

Jump to: navigation, search

Uso del DataReader

public List<Alarm> GetAlarm()
{
	//...
	List<Alarm> alarms = new List<Alarm>();
 
	SqlCommand command = new SqlCommand();
	command.Connection = connection;
	command.CommandType = System.Data.CommandType.Text;
	string qrySQL = @"SELECT 
					* FROM Alarms with(nolock)
					WHERE 1=1";
 
	using (SqlDataReader reader = command.ExecuteReader())
	{
		while (reader.Read())
		{
			Alarm alarm = FromReaderToAlarm(reader);
			alarms.Add(alarm);
		}
	}
	//...
}
 
private static Alarm FromReaderToAlarm(SqlDataReader reader)
{
	Alarm alarm = new Alarm();
 
	alarm.Acknowledged = (Boolean)reader["Acknowledged"];
	alarm.AcknowledgeDate = DateHelper.SpecifyUtcKind(reader["AcknowledgeDate"] as DateTime?);
	alarm.Visible = (Boolean)reader["Visible"];
	alarm.AlarmDate = DateHelper.SpecifyUtcKind(reader["AlarmDate"] as DateTime?);
	alarm.CompanyCode = reader["CompanyCode"] as string;
	alarm.PositionId = reader["PositionId"] as Guid?;
	alarm.GeoPosition = reader["GeoPosition"] as SqlGeometry;
	alarm.Id = (Guid)reader["Id"];
	alarm.Opened = (Boolean)reader["Opened"];
	alarm.Segment1Id = reader["Segment1Id"] as Guid?;
	alarm.Segment2Id = reader["Segment2Id"] as Guid?;
	alarm.Severity = (int)reader["Severity"];
	alarm.ShipCode = reader["ShipCode"] as string;
	alarm.Type = reader["Type"] as string;
	alarm.CreateDate = (DateTime)reader["CreateDate"];
	alarm.EditDate = (DateTime)reader["EditDate"];
	alarm.AlarmCounter = (int)reader["AlarmCounter"];
	alarm.ShipLocalTime = reader["ShipLocalTime"] as DateTime?;
	alarm.PositionDateUTC = DateHelper.SpecifyUtcKind(reader["PositionDateUTC"] as DateTime?);
	alarm.ClosedDateUTC = DateHelper.SpecifyUtcKind(reader["CloseDateUTC"] as DateTime?);
	alarm.ExtraEntityId = reader["ExtraEntityId"] as Guid?;
	return alarm;
}



C# | Comunicazioni col DB


Visual Studio

Author