对象关系映射 - 插入更新存储过程

时间:2011-10-22 08:32:51

标签: c# generics reflection orm

我正在尝试学习对象关系映射,并创建了我自己的接受对象的方法,并将列名映射到对象中的各自属性。

我想在插入时通过仅指定存储过程并将对象的属性名称映射到相应的列名称来执行相同的操作。但我不确定我怎么能这样做,或者它是否可能?有谁知道任何好的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;
    }
}

2 个答案:

答案 0 :(得分:1)

目前尚不清楚您遇到了什么问题,但这里有几点指示:

  1. 您可以看到a sample here of how to get info on the parameters of a stored procedure这要求您准备好与其参数匹配的存储过程。

  2. 如果需要,您可以通过storing the data in XML format in the database创建更灵活的系统。这样您就不必将实体映射到存储过程和/或表。

答案 1 :(得分:0)

由于标记答案中提供的链接,我最终能够将对象属性映射到具有相同名称的存储过程参数。我已经提供了以下代码。

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();
                    }
                }        
            }
        }