将自定义类转换为对象并返回

时间:2013-12-11 15:47:12

标签: c# linq ms-access

我试图尽可能一般地编写一个函数(创建和填充数据库表)。

我的问题是我想在不同的课程中使用它。问题是,为了使它非常通用,我必须将我的自定义类数据作为对象列表传递给函数,然后转换回来。

这就是我所拥有的:

public static void appenddatatotableRef(string connectionstring, string tablename, List<Object> values, string objecttype)
    {
        var properties = new List<System.Reflection.FieldInfo>();

        if (objecttype == "PowerResults")
        {
            var data = values.OfType<PowRes>().ToList();
            properties = typeof(PowRes).GetFields(BindingFlags.Public | BindingFlags.Instance).OrderBy(x => x.Name).ToList();
        }

        string columnam = "[" + properties[0].Name + "]";
        string valstring = "@" + properties[0].Name;

        for (int i = 1; i < properties.Count; i++)
        {
            columnam = columnam + ", [" + properties[i].Name + "]";
            valstring = valstring + ", @" + properties[i].Name;
        }


        string commandtext = "INSERT INTO [" + tablename + "](" + columnam + ")" + "VALUES(" + valstring + ")";

        using (var myconn = new OleDbConnection(connectionstring))
        {
            myconn.Open();
            using (var cmd = new OleDbCommand())
            {

                foreach (var item in values)
                {
                    cmd.CommandText = commandtext;
                    cmd.Parameters.Clear();
                    for (int i = 1; i < properties.Count; i++)
                    {
                        try
                        {
                            cmd.Parameters.AddWithValue("@" + properties[i].Name, properties[i].GetValue(item));
                        }
                        catch
                        {

                        }
                    }

                    cmd.Connection = myconn;
                    cmd.ExecuteNonQuery();

                }
            }
            myconn.Close();
        }

    }

如果我将输入更改为List<PowerRes>并摆脱从值到数据的转换但是它不再是通用的,那么这可以正常工作。

我能以某种方式完全通用吗?

工作解决方案(感谢各位指导我指向正确的方向)!

public static void appenddatatotableRef<T>(string connectionstring, string tablename, IEnumerable<T> values)
    {

        var properties = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance).OrderBy(x => x.Name).ToList();

        //setting up the command text for insert into
        string columnam = "[" + properties[0].Name + "]";
        string valstring = "@" + properties[0].Name;
        for (int i = 1; i < properties.Count; i++)
        {
            columnam = columnam + ", [" + properties[i].Name + "]";
            valstring = valstring + ", @" + properties[i].Name;
        }
        string commandtext = "INSERT INTO [" + tablename + "](" + columnam + ")" + "VALUES(" + valstring + ")";


        using (var myconn = new OleDbConnection(connectionstring))
        {
            myconn.Open();
            using (var cmd = new OleDbCommand())
            {

                foreach (var item in values)
                {
                    cmd.CommandText = commandtext;
                    cmd.Parameters.Clear();
                    for (int i = 0; i < properties.Count; i++)
                    {
                            cmd.Parameters.AddWithValue("@" + properties[i].Name, properties[i].GetValue(item) ?? "");
                    }

                    cmd.Connection = myconn;
                    cmd.ExecuteNonQuery();

                }
            }
            myconn.Close();
        }

    }

2 个答案:

答案 0 :(得分:3)

你需要制作一个通用函数:

public static void AppendToTable<T>(..., IEnumerable<T> items, ...) {
    var properties = typeof(T).GetFields();

答案 1 :(得分:1)

可以做到。一些名为“MicroORMs”的现有开源库可以做得很好。

请查看PetaPoco示例。