/// <summary>
/// 接口基类
/// </summary>
/// <typeparam name="T">泛型</typeparam>
public interface IBaseAccess<T> where T : class
{
int Add(T obj);
int Update(T obj);
T GetModel(T obj);
int Delete(T obj);
List<T> GetList();
}
public abstract class IPager<T> where T : class, new()
{
/// <summary>
/// 获取从读取流转换为实体对象
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
#region DoTransferType
public T DoTransferType(IDataReader dr)
{
T model = new T();
int count = dr.FieldCount;
PropertyInfo[] property_lst = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in property_lst)
{
for (int i = 0; i < count; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
string name = dr.GetName(i).ToUpper();
if (name.Equals(property.Name.ToUpper()))
{
property.SetValue(model, dr.GetValue(i), null);
break;
}
}
}
}
return model;
}
#endregion
/// <summary>
/// 获取从实体对象转换参数集合
/// </summary>
/// <param name="model">实体对象</param>
/// <returns>泛型参数集合</returns>
#region DoTransferParameter
public List<SqlParameter> DoTransferParameter(T model)
{
List<SqlParameter> arylst = new List<SqlParameter>();
PropertyInfo[] property_arylst = model.GetType().GetProperties();
foreach (PropertyInfo info in property_arylst)
{
if (info.GetValue(model, null) != null
&& !info.PropertyType.FullName.ToUpper().Contains("Entity"))
{
string value = info.GetValue(model, null).ToString();
SqlParameter para = new SqlParameter("@" + info.Name, info.PropertyType.FullName);
para.Value = value;
arylst.Add(para);
}
}
return arylst;
}
}
[Serializable]实体中要写这样子的 标示 和数据库要对应
public static int ExecuteNonQueryExpand(string connectionString, CommandType commandType, string commandText, List<SqlParameter> commandParameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
return ExecuteNonQueryExpand(connection, commandType, commandText, commandParameters);
}
}
public static int ExecuteNonQueryExpand(SqlConnection connection, CommandType commandType, string commandText, List<SqlParameter> commandParameters)
{
SqlCommand command = new SqlCommand();
PrepareCommandExpand(command, connection, null, commandType, commandText, commandParameters);
int num = command.ExecuteNonQuery();
command.Parameters.Clear();
return num;
}
private static void PrepareCommandExpand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, List<SqlParameter> commandParameters)
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
command.Connection = connection;
command.CommandText = commandText;
if (transaction != null)
{
command.Transaction = transaction;
}
command.CommandType = commandType;
if (commandParameters != null)
{
AttachParameters(command, commandParameters);
}
}
private static void AttachParameters(SqlCommand command, List<SqlParameter> commandParameters)
{
foreach (SqlParameter parameter in commandParameters)
{
if ((parameter.Direction == ParameterDirection.InputOutput) && (parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}