We have created an internal tool that generates all access to data, each table has a class representing its data and all common operations. (think of a lightweight Entity structure).
These DataAccess objects always have a constructor that receives the connection string, and a load function that receives the SqlDataReader.
Something like that:
class Customer
{
public string ConnStr;
public int Id;
public string Name;
Public customers(string connStr)
{
ConnStr = connStr;
}
public Customer Load(SqlDataReader)
{
if(reader.Read())
{
Id = reader["Id"].ToString();
Name = reader["Name"].ToString();
}
}
}
I want to write a Data Access static method utility that will allow me to write my SQL and get a list of objects in response, following the example of the previous object:
string SQL = "SELECT * FROM Customers WHERE Name=@Name";
List<Customer> customers = GetList<Customer>(connStr, SQL, new SqlParameters("@Name", "John"));
- , , , , generics - , ( + load), , , :
public static List<T> GetList<T>(string connStr, string SQL, params SqlParameter[] prms)
{
List<T> list = new List<T>();
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(SQL, conn);
foreach (SqlParameter param in prms)
{
cmd.Parameters.Add(param);
}
using (SqlDataReader reader = cmd.ExecuteReader())
{
}
}
return list;
}
Btw, DataAccess, - + javascript, javascript (, , ).
- , " " , , , , : eytan@titkadem.co.il
,