简体   繁体   English

如何返回与数据库中的记录对应的对象列表?

[英]How can I return a list of objects corresponding to records in my database?

I am new to C# and I am struggling with everything, please help. 我是C#的新手,我在所有方面都在挣扎,请提供帮助。 Here is what I am trying to do: 这是我正在尝试做的事情:

Create a database Class which returns a dynamic list which contains instances of a class depending on the table being queried: 创建一个数据库类,该类返回一个动态列表,该列表包含一个类的实例,具体取决于要查询的表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;

    Class MYDatabase{

    public void main ()
    {
         String QueryString="select COLUMN_NAME, DATA_TYPE from information_schema.columns where table_name = 'mytable'";
         Object MyData=GetRsults(QueryString,"SchemaStructure");
    }

    public static Object GetResults(string QueryString, String ClassName)
    {

        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;

        cmd.CommandText = QueryString;
        cmd.CommandType = CommandType.Text;
        //assuming I already defined my connection
        cmd.Connection = Connection;

        Connection.Open();

        reader = cmd.ExecuteReader();

        Type ClassType = Type.GetType(ClassName);
        Type ListType = typeof(List<>).MakeGenericType(new Type[] { ClassType });
        Object Results = Activator.CreateInstance(ListType);
        Object SingleResult = Activator.CreateInstance(ClassType);

        while (reader.Read())
        {

            for (int i = 0; i < reader.FieldCount; i++)
            {
                var PropName= SingleResult.GetType().GetProperty(reader.GetName(i).ToString());
                //Convert.ChangeType(value, propertyInfo.PropertyType)
                PropName.SetValue(PropName,reader.GetValue(i).ToString(),null);

            }
            Results.Add(SingleResult);

        }
        Connection.Close();

        return Results;
    }
    }

class SchemaStructure
{
    public string COLUMN_NAME { set; get; }
    public string DATA_TYPE { set; get; }

}

This is not working, the line Results.Add(SingleResult); 这行不通, Results.Add(SingleResult);Results.Add(SingleResult); gives me a message 给我一个信息

'object' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) “对象”不包含“添加”的定义,并且找不到包含接受“对象”类型的第一个参数的扩展方法“添加”(您是否缺少using指令或程序集引用?)

Can anyone please help me resolve this code? 谁能帮我解决此代码?

I am open to other suggestions if my approach doesn't make sense at all. 如果我的方法根本没有道理,我愿意接受其他建议。

Thanks 谢谢

I think foremost, I wouldn't hand roll my POCO objects when you have the Entity Framework at your disposal. 我想,最重要的是,当您拥有实体框架时,我不会手摇我的POCO对象。 Use NuGet to add the latest release of Entity to your project, then you can right click your project and use Entity's "Reverse Engineer Code First" feature to build out your models. 使用NuGet将Entity的最新版本添加到项目中,然后可以右键单击项目并使用Entity的“ Reverse Engineer Code First”功能来构建模型。 This of course assumes that you're using a DB that Entity can connect to, and that the DB has been built already... 当然,这假设您使用的是Entity可以连接到的数据库,并且该数据库已经构建完毕...

You can read about EF here: http://msdn.microsoft.com/en-us/data/ef.aspx 您可以在此处阅读有关EF的信息: http : //msdn.microsoft.com/en-us/data/ef.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 go 如何在插入不同 ID 对应于相同名称的记录列表后修复我的数据库表 - How do I go about fixing my database table after inserting a list of records different id's corresponding to same name 如何通过LINQ to SQL更新和删除数据库中的记录? - How can I Update and delete records in my database by LINQ to SQL? 如何从 FormCollection 返回对象列表? - How can I return a list of objects from a FormCollection? 如何将GroupBy与对象列表一起使用并返回IList <T> ? - How can i use GroupBy with a list of objects and return IList<T>? 当func &lt;&gt;的列表处理不同的对象时,如何接受Func &lt;&gt;委托的列表并返回列表的列表 - How can I accept a list of Func<> delegates and return a list of list when the List of func<> processes different objects 在Umbraco中,如何将对象的通用列表绑定到我的视图? - In Umbraco, how can I bind a generic list of Objects to my View? 如何将我所有的策略模式对象注入到一个列表中? - How Can I Inject All my Strategy Pattern Objects Into a List? 如何使用Entity Framework生成此查询以返回相应的集合? - How can I generate this query with Entity Framework to return the corresponding collections? 我可以将对象添加到Access数据库中吗? - Can I add objects to my Access database? 如何从我的数据库中返回数据列表? - How to return a list of data from my database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM