对象关系映射 - 插入更新存储过程
我正在尝试学习对象关系映射,并制作了自己的方法来接受对象并将列名称映射到对象中相应的属性。
我想在插入时通过仅指定存储过程并将对象的属性名称映射到相应的列名称来执行相同的操作。但我不确定我该如何解决这个问题,或者是否可能?有谁知道任何好的 c# 对象关系映射教程。
我在下面为我的读者提供了一个列表。
我这样做只是为了好玩,所以我不想使用任何 ORM 框架。
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;
}
}
I'm trying to learn object relational mapping and made my own method that accepts an object and maps column names to its respective properties in the object.
I would like to do the same when inserting by just specifying the stored procedure and map the property names of the object to the respective column names. But I'm unsure how I can go about this or if its even possible? Does anyone know any good c# object relational mapping tutorials.
I've provided my reader below which populates a list.
I'm just doing this for fun so I don't want to use any ORM frameworks.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚您遇到了什么问题,但这里有一些提示:
您可以看到这里是如何获取存储过程参数信息的示例 这要求您拥有已准备好与其参数匹配的存储过程。
如果需要,您可以通过将数据以 XML 格式存储在数据库中来创建更灵活的系统< /a>.这样您就不必将实体映射到存储过程和/或表。
It is not quite clear what problem you are running into but here are a couple of pointers:
You can see a sample here of how to get info on the parameters of a stored procedure This requires you to have the stored procedure matching its parameters ready.
If you want, you could create a more flexible system by storing the data in XML format in the database. that way you would not have to map the entities to stored procedures and or tables.
感谢标记答案中提供的链接,我最终能够将对象属性映射到具有相同名称的存储过程参数。我提供了下面的代码。
Thanks to the links provided in the marked answer I ended up being able to map an objects properties to a stored procedures parameters that have the same names. I've provided the code below.