将SQL Server的多个表映射到Class [英] Map multiple table of sql server to Class

查看:105
本文介绍了将SQL Server的多个表映射到Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在sql server中创建了一个存储过程,该存储过程从多个表返回多个select语句.现在,我需要将这些多个表的记录映射到单个类.即我有两个表用户,地址.现在,我有一个存储过程正在返回那些表的数据

选择*来自用户
选择*从地址

现在,我需要将这两个表的记录绑定到单个类.我不想使用数据表.有人让我知道如何实现吗?

Hi All,

I have created a stored procedure in sql server which is returning multiple select statements from multiple table. Now I need to map those multiple table''s record to a single class. i.e. I have two tables User, Address. Now I have a stored procedure which is returning those table''s data

SELECT * FROM USERS
SELECT * FROM ADDRESS

Now I need to bind these two table''s record to a Single Class. I do not want to use Datatable. Anyone let me know how to achieve this?

推荐答案

我要设置的方式是先创建一个Address类,然后再创建一个User类,该类具有一个返回一个地址,类似这样

The way I would set this up would be to have an Address class, then a User class that has a property that returns an Address, something like this

class Address
    {
        public string Street { get; set; }
        public string Suburb { get; set; }
        public string City { get; set; }
    }

    class User
    {
        public string FirstName { get; set; }
        public string LastName{get;set}
        public int Age{get;set;}
        public Address CurrentAddress{get;set;}
    }



然后,您可以从一个DataReader中填充这两个类.

希望这对您有帮助



Then you can populate both classes from one DataReader.

Hope this helps


您好,谢谢您的回复.但是我没有使用datareader.我已将存储过程与Linq映射,该Linq返回记录列表.
Hi, Thanks for your repaly..but I am not using datareader. I have mapped the stored procedure with the Linq, which return the List of records.


这篇关于将SQL Server的多个表映射到Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆