Questa è la classe che gestisce la collection:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
namespace MTC.DBObject
{
public class News : DBConnector
{
private List<NewsItem> _Items = new List<NewsItem>();
/// <summary>
/// Ritorna un'istanza di NewsItem in base all'id
/// </summary>
public NewsItem Get(int IDNews)
{
using (SqlCommand objCmd = new SqlCommand("nomeproc", this.OpenConnection()))
{
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.Add("@IDNews", SqlDbType.Int).Value = IDNews;
SqlDataReader objDR = objCmd.ExecuteReader();
NewsItem ni = new NewsItem();
while (objDR.Read())
{
ni.IDNews = Convert.ToInt32(objDR["IDNews"]);
ni.Contenuto = objDR["Contenuto"].ToString();
ni.Titolo = objDR["Titolo"].ToString();
ni.Data = Convert.ToDateTime(objDR["Data"]);
if (objDR["Foto"] is System.DBNull)
{ }
else
{ ni.Foto = (byte[])objDR["Foto"]; }
}
this.CloseConnection();
if (ni != null)
{
return ni;
}
else
{
return null;
}
}
}
/// <summary>
/// Ritorna le news in evidenza
/// </summary>
public List<NewsItem> GetInEvidenza()
{
using (SqlCommand objCmd = new SqlCommand("nomeproc", this.OpenConnection()))
{
objCmd.CommandType = CommandType.StoredProcedure;
SqlDataReader objDR = objCmd.ExecuteReader();
// ricavo i dati dal db e li ciclo per riempire la collezione
while (objDR.Read())
{
NewsItem ni = new NewsItem();
ni.IDNews = Convert.ToInt32(objDR["IDNews"]);
ni.Titolo = objDR["Titolo"].ToString();
ni.Abstract = objDR["Abstract"].ToString();
ni.Data = Convert.ToDateTime(objDR["Data"]);
ni.Contenuto = objDR["Contenuto"].ToString();
ni.Foto = (byte[])objDR["Foto"]; }
ni.Thumbnail = (byte[])objDR["Thumbnail"];
// aggiungo l'item alla collection
this._Items.Add(ni);
}
this.CloseConnection();
if (this._Items != null && this._Items.Count > 0)
{return this._Items;}
else
{return null;}
}
}
}
}
e questa è la Item, ovvero la riga del db (la tua riga di anagrafica):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace MTC.DBObject
{
public class NewsItem
{
private int _IDNews;
private DateTime _Data;
private String _Titolo;
private String _Abstract;
private byte[] _Foto;
private byte[] _Thumbnail;
private String _Contenuto;
public int IDNews {
get {
return _IDNews;
}
set {
_IDNews = value;
}
}
public DateTime Data
{
get
{
return _Data;
}
set
{
_Data = value;
}
}
public String Titolo
{
get
{
return _Titolo;
}
set
{
_Titolo = value;
}
}
public String Abstract
{
get
{
return _Abstract;
}
set
{
_Abstract = value;
}
}
public byte[] Foto
{
get
{
return _Foto;
}
set
{
_Foto = value;
}
}
public byte[] Thumbnail
{
get
{
return _Thumbnail;
}
set
{
_Thumbnail = value;
}
}
public String Contenuto
{
get
{
return _Contenuto;
}
set
{
_Contenuto = value;
}
}
}
}
Quest'ultima ha solo le proprietà che mappano l'oggetto, quindi i campi.
Spero possa esserti di aiuto, ciao!
--
Alessandro Alpi | SQL Server MVP
http://www.alessandroalpi.net
http://blogs.dotnethell.it/suxstellino
http://mvp.support.microsoft.com/profile/Alessandro.Alpi
http://italy.mvps.org