简体   繁体   中英

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. 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); 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?)

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. 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. This of course assumes that you're using a DB that Entity can connect to, and that the DB has been built already...

You can read about EF here: http://msdn.microsoft.com/en-us/data/ef.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM