[英]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.