C# 对象关系映射-插入更新存储过程
我正在尝试学习对象关系映射,并创建了自己的方法,该方法接受对象并将列名映射到对象中相应的属性 我希望在插入时也这样做,只需指定存储过程并将对象的属性名映射到相应的列名。但我不确定我该怎么做,或者这是否可能?有人知道什么好的c#对象关系映射教程吗 我在下面提供了我的阅读器,它填充了一个列表 我这样做只是为了好玩,所以我不想使用任何ORM框架C# 对象关系映射-插入更新存储过程,c#,generics,reflection,orm,C#,Generics,Reflection,Orm,我正在尝试学习对象关系映射,并创建了自己的方法,该方法接受对象并将列名映射到对象中相应的属性 我希望在插入时也这样做,只需指定存储过程并将对象的属性名映射到相应的列名。但我不确定我该怎么做,或者这是否可能?有人知道什么好的c#对象关系映射教程吗 我在下面提供了我的阅读器,它填充了一个列表 我这样做只是为了好玩,所以我不想使用任何ORM框架 public static List<T> loadFromReader(string sProcName) { List<T>
public static List<T> loadFromReader(string sProcName)
{
List<T> EntityCollection = new List<T>();
Type type = typeof(T);
PropertyInfo[] properties = typeof(T).GetProperties();
using (Connection)
{
SqlCommand command = new SqlCommand(sProcName);
command.CommandType = CommandType.StoredProcedure;
if (SqlParamterList != null && SqlParamterList.Count > 0)
{
foreach (SqlParameter parameter in SqlParamterList)
command.Parameters.Add(parameter);
}
using (SqlDataReader reader = command.ExecuteReader())
{
int columnCount = reader.FieldCount;
string[] columnName = new string[columnCount];
for (int i = 0; i <= columnCount - 1; i++)
columnName[i] = reader.GetName(i);
while (reader.Read())
{
object obj = Activator.CreateInstance(type);
for (int i = 0; i <= columnName.Length - 1; i++)
{
foreach (PropertyInfo property in properties)
{
if (property.Name.Contains(columnName[i]))
{
object value = reader[i];
if (value == DBNull.Value)
value = null;
type.GetProperty(property.Name).SetValue(obj, value, null);
}
}
}
EntityCollection.Add((T)obj);
}
}
clearParameters();
return EntityCollection;
}
}
公共静态列表loadFromReader(字符串存储名称)
{
List EntityCollection=新列表();
类型=类型(T);
PropertyInfo[]properties=typeof(T).GetProperties();
使用(连接)
{
SqlCommand=newsqlcommand(sProcName);
command.CommandType=CommandType.storedProcess;
if(sqlparametrlist!=null&&sqlparametrlist.Count>0)
{
foreach(SqlParameterList中的SqlParameter参数)
command.Parameters.Add(参数);
}
使用(SqlDataReader=command.ExecuteReader())
{
int columnCount=reader.FieldCount;
string[]columnName=新字符串[columnCount];
对于(int i=0;i来说,不太清楚您遇到了什么问题,但这里有几个指针:
您可以看到,这要求您准备好与参数匹配的存储过程
如果您愿意,您可以通过创建一个更灵活的系统,这样您就不必将实体映射到存储过程和/或表
由于标记答案中提供的链接,我最终能够将对象属性映射到具有相同名称的存储过程参数
public static void insertObjectMapper(string sProcName, List<T> entities)
{
Type type = typeof(T);
PropertyInfo[] properties = typeof(T).GetProperties();
string[] paramNames = null;
using (SqlCommand command = new SqlCommand(sProcName, Connection))
{
command.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(command);
if (command.Parameters != null || command.Parameters.Count > 0)
{
clearParameters();
foreach (SqlParameter parameter in command.Parameters)
addParameter(parameter.ParameterName, parameter.SqlDbType, parameter.Direction, parameter.Value);
paramNames = new string[SqlParamterList.Count];
int count = 0;
foreach (SqlParameter parameter in SqlParamterList)
{
paramNames[count] = parameter.ParameterName.Substring(1);
++count;
}
foreach (T entity in entities)
{
for (int i = 0; i <= paramNames.Length - 1; i++)
{
foreach (PropertyInfo property in properties)
{
if (property.Name.Contains(paramNames[i]))
{
foreach (SqlParameter parameter in SqlParamterList)
{
if (parameter.ParameterName.Substring(1).Contains(paramNames[i]))
{
parameter.Value = entity.GetType().GetProperty(paramNames[i]).GetValue(entity, null);
break;
}
}
}
}
}
command.Parameters.Clear();
foreach (SqlParameter parameter in SqlParamterList)
command.Parameters.Add(parameter);
command.ExecuteNonQuery();
}
}
}
}
public静态void insertObjectMapper(字符串存储名称,列出实体)
{
类型=类型(T);
PropertyInfo[]properties=typeof(T).GetProperties();
字符串[]paramNames=null;
使用(SqlCommand=newsqlcommand(sProcName,Connection))
{
command.CommandType=CommandType.storedProcess;
SqlCommandBuilder.DeriveParameters(命令);
if(command.Parameters!=null | | command.Parameters.Count>0)
{
clearParameters();
foreach(command.Parameters中的SqlParameter参数)
addParameter(parameter.ParameterName、parameter.SqlDbType、parameter.Direction、parameter.Value);
paramNames=新字符串[sqlparametrlist.Count];
整数计数=0;
foreach(SqlParameterList中的SqlParameter参数)
{
paramNames[count]=parameter.ParameterName.Substring(1);
++计数;
}
foreach(实体中的T实体)
{
对于(int i=0;i