简体   繁体   English

从数据表创建C#对象

[英]C# object creation from datatable

I'm just getting my head round C#. 我只是围绕着C#。 I've been creating classes and objects so say i created a class called Member: 我一直在创建类和对象,所以说我创建了一个名为Member的类:

 public class Member
 {
     public int MemberID;
     public string FirstName;
     public string LastName;
     public string UserName;
 }

and I create a new object of that class by doing this: 我通过这样做创建该类的新对象:

    Member Billy = new Member();

    Billy.UserName = "Jonesy";
    Billy.FirstName = "Billy";
    Billy.LastName = "Jones";

That's all fine but what if I've queried a database and gotten back 5 members, can I create objects on the fly? 这一切都很好,但如果我查询了一个数据库并找回了5个成员,我可以动态创建对象吗? Or what is the best way to store these members in memory? 或者将这些成员存储在内存中的最佳方法是什么?

I've used VB.Net where I would just add them into a datatable. 我已经使用VB.Net,我只是将它们添加到数据表中。 But I've never really done any object-oriented programming before and thought since I'm learning C#, now's the best time to learn OOP. 但是我从来没有真正做过任何面向对象的编程,因为我正在学习C#,现在是学习OOP的最佳时机。

This is a common problem. 这是一个常见问题。 Fortunately there is a good answer to this: Linq To Sql! 幸运的是有一个很好的答案:Linq To Sql! You can read about it here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx 你可以在这里阅读: http//weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

What it basically does is that it creates a class, one per table you choose in your database. 它基本上做的是它创建一个类,每个表在数据库中选择一个。 This makes it very easy to get all your objects directly from the database into object oriented programming. 这使得将所有对象直接从数据库中获取到面向对象的编程变得非常容易。

Saving is as easy as calling a function "SubmitChanges()". 保存就像调用函数“SubmitChanges()”一样简单。 There are more providers for this but I think Linq will suit you well as a beginner as it abstracts a lot of the annoying parts. 有更多的提供商,但我认为Linq作为一个初学者很适合你,因为它抽象了许多令人讨厌的部分。

I'd recommend that you look at LINQ to SQL. 我建议您查看LINQ to SQL。 Then you can write code like this to query the database to get a specific user: 然后你可以编写这样的代码来查询数据库以获取特定用户:

Member member = db.Members.Single(member => member.UserName == "Jonesy");

or to get users matching a criterion: 或者让用户符合标准:

IQueryable<Member> members = db.Members
    .Where(member => member.LastName == "Jones");

LINQ to SQL also takes care of writing the boilerplate code to declare the classes based on the database structure. LINQ to SQL还负责编写样板代码,以根据数据库结构声明类。

If you don't go with LINQ to SQL (or the Entity Framework) then using a regular ADO.NET DataReader you would loop through the results, instantiate a new object with the details, and add it to a list. 如果不使用LINQ to SQL(或实体框架),那么使用常规ADO.NET DataReader,您将遍历结果,使用详细信息实例化新对象,并将其添加到列表中。

Roughly it would look like this: 粗略地看起来像这样:

List<Member> members = new List<Member>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (SqlCommand command = new SqlCommand(queryString, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Member member = new Member();
                member.UserName = reader.GetString(0);
                member.FirstName = reader.GetString(1);
                member.LastName = reader.GetString(2);
                members.Add(member);
            }
        }
    }
}

foreach(Member member in members)
{
    // do something
}

Linq2Sql suggested twice is not the only way, but having in mind case when all objects can be one to one mapped to tables in your database it works just fine. Linq2Sql建议两​​次不是唯一的方法,但考虑到当所有对象可以一对一映射到数据库中的表时它工作得很好。 Personally I would go for EF instead however, because it allows you to have one more layer of abstraction. 我个人会选择EF,因为它允许你有一层抽象。

Also I can suggest you to look at db4o and etc, there you just can save you poco and that's all. 另外我建议你看看db4o等,在那里你可以省下你的poco,就是这样。

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

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