Ho scaricato un esempio da qui "http://www.codeproject.com/KB/database/DBaseFactGenerics.aspx",con delle classi per l'accesso ai dati ma non sò come utilizzarlo, mi piacerebbe tanto saperlo interpretare , se qualcuno mi potesse aiutare mi farebbe piacere,posto di seguito il contenuto dei file.
Come faccio ad utilizzarlo per esempio recuperare dati da una tabella?
FILE: DataBase.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
namespace Global.Data
{
public abstract class DataBase<TParameter,TDataReader,TConnection, TTransaction,TDataAdapter,TCommand> : IDisposable
where TParameter : DbParameter,IDataParameter
where TDataReader : DbDataReader,IDataReader
where TConnection : DbConnection,IDbConnection,new()
where TTransaction : DbTransaction,IDbTransaction
where TDataAdapter : DbDataAdapter,IDataAdapter,new()
where TCommand : DbCommand, IDbCommand, new()
{
protected TConnection _conn;
protected TTransaction _trans;
public DataBase(string ConnectionString)
{
_conn = new TConnection();
_conn.ConnectionString = ConnectionString;
}
public TConnection Connection
{
get
{
return _conn;
}
}
public TTransaction Transaction
{
get
{
return _trans;
}
}
protected DataSet ExecuteDataSet(string StoreProcName, List<TParameter> Params)
{
bool internalOpen = false;
DataSet ds = null;
TDataAdapter da;
TCommand cmd;
try
{
ds = new DataSet();
da = new TDataAdapter();
cmd = new TCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = StoreProcName;
if (_trans != default(TTransaction))
cmd.Transaction = _trans;
else
cmd.Connection = (TConnection)_conn;
if (Params != null || Params.Count > 0)
{
foreach (DbParameter param in Params)
cmd.Parameters.Add(param);
}
da.SelectCommand = cmd;
if (_conn.State == ConnectionState.Closed)
{
_conn.Open();
internalOpen = true;
}
da.Fill(ds);
return ds;
}
catch (DbException DbEx)
{
throw DbEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (internalOpen)
_conn.Close();
}
}
protected DataSet ExecuteDataSet(string StoreProcName)
{
return ExecuteDataSet(StoreProcName, null);
}
protected T ExecuteScalar<T>(string StoreProcName, List<TParameter> Params)
{
bool internalOpen = false;
TCommand cmd;
try
{
cmd = new TCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = StoreProcName;
if (_trans != default(TTransaction))
cmd.Transaction = _trans;
else
cmd.Connection = _conn;
if (Params != null || Params.Count > 0)
{
foreach (DbParameter param in Params)
cmd.Parameters.Add(param);
}
if (_conn.State == ConnectionState.Closed)
{
_conn.Open();
internalOpen = true;
}
object retVal = cmd.ExecuteScalar();
if (retVal is T)
return (T)retVal;
else if (retVal == DBNull.Value)
return default(T);
else
throw new Exception("Object returned was of the wrong type.");
}
catch (DbException DbEx)
{
throw DbEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (internalOpen)
_conn.Close();
}
}
protected T ExecuteScalar<T>(string StoreProcName)
{
return ExecuteScalar<T>(StoreProcName);
}
protected int ExecuteNonQuery(string StoreProcName, List<TParameter> Params)
{
bool internalOpen = false;
TCommand cmd;
try
{
cmd = new TCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = StoreProcName;
if (_trans != default(TTransaction))
cmd.Transaction = _trans;
else
cmd.Connection = _conn;
if (Params != null || Params.Count > 0)
{
foreach (DbParameter param in Params)
cmd.Parameters.Add(param);
}
if (_conn.State == ConnectionState.Closed)
{
_conn.Open();
internalOpen = true;
}
return cmd.ExecuteNonQuery();
}
catch (DbException DbEx)
{
throw DbEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (internalOpen)
_conn.Close();
}
}
protected int ExecuteNonQuery(string StoreProcName)
{
return ExecuteNonQuery(StoreProcName, null);
}
protected TDataReader ExecuteReader(string StoreProcName, List<TParameter> Params)
{
bool internalOpen = false;
TCommand cmd;
try
{
cmd = new TCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = StoreProcName;
if (_trans != default(TTransaction))
cmd.Transaction = _trans;
else
cmd.Connection = _conn;
if (Params != null || Params.Count > 0)
{
foreach (DbParameter param in Params)
cmd.Parameters.Add(param);
}
if (_conn.State == ConnectionState.Closed)
{
_conn.Open();
internalOpen = true;
}
return (TDataReader)cmd.ExecuteReader();
}
catch (DbException DbEx)
{
throw DbEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (internalOpen)
_conn.Close();
}
}
protected TDataReader ExecuteReader(string StoreProcName)
{
return ExecuteReader(StoreProcName);
}
public bool BeginTransaction()
{
if (_conn != null && _conn.State == ConnectionState.Closed && _trans == null)
{
_conn.Open();
_trans = (TTransaction)_conn.BeginTransaction();
return true;
}
return false;
}
public bool RollBackTransaction()
{
if (_conn != null && _conn.State == ConnectionState.Open && _trans != null)
{
_trans.Rollback();
_conn.Close();
_trans.Dispose();
_trans = default(TTransaction);
return true;
}
return false;
}
public bool CommitTransaction()
{
if (_conn != null && _conn.State == ConnectionState.Open && _trans != null)
{
_trans.Commit();
_conn.Close();
_trans.Dispose();
_trans = default(TTransaction);
return true;
}
return false;
}
public abstract bool RollBackTransaction(string SavePointName);
public abstract bool SaveTransactionPoint(string SavePointName);
#region IDisposable Members
public void Dispose()
{
if (_conn != null)
{
if (_trans != null)
{
_trans.Rollback();
_trans.Dispose();
}
if (_conn.State != ConnectionState.Closed)
_conn.Close();
_conn.Dispose();
}
}
~DataBase()
{
Dispose();
}
#endregion
}
}
FILE MyDataBaseLayer
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
namespace Global.Data
{
public class MyDataBaseLayer<TParameter, TDataReader, TConnection, TTransaction, TDataAdapter, TCommand> : DataBase<TParameter, TDataReader, TConnection, TTransaction, TDataAdapter, TCommand>
where TParameter : DbParameter, IDataParameter,new()
where TDataReader : DbDataReader, IDataReader
where TConnection : DbConnection, IDbConnection, new()
where TTransaction : DbTransaction, IDbTransaction
where TDataAdapter : DbDataAdapter, IDataAdapter, new()
where TCommand : DbCommand, IDbCommand, new()
{
protected List<TParameter> _params;
public MyDataBaseLayer(string ConnectionString):base(ConnectionString)
{}
public Guid InsertNeMember(string MemberName)
{
TParameter paramMemberName = new TParameter();
paramMemberName.ParameterName = "@memberName";
paramMemberName.Value = MemberName;
paramMemberName.DbType = System.Data.DbType.VarNumeric;
_params = new List<TParameter>();
_params.Add(paramMemberName);
return ExecuteScalar<Guid>("proc_Member_Insert",_params);
}
public override bool RollBackTransaction(string SavePointName)
{
throw new Exception("The method or operation is not implemented.");
}
public override bool SaveTransactionPoint(string SavePointName)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
FILE SqlDataBase
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Global.Data
{
public class SqlDataBase : DataBase<SqlParameter,SqlDataReader,SqlConnection,SqlTransaction,SqlDataAdapter,SqlCommand>
{
public SqlDataBase(string ConnectionString)
: base(ConnectionString)
{
}
public override bool RollBackTransaction(string SavePointName)
{
if (_conn != null && _conn.State == ConnectionState.Open && _trans != null)
{
_trans.Rollback(SavePointName);
return true;
}
return false;
}
public override bool SaveTransactionPoint(string SavePointName)
{
if (_conn != null && _conn.State == ConnectionState.Open && _trans != null)
{
_trans.Save(SavePointName);
return true;
}
return false;
}
}
}