将数据从数据库获取到类对象数组中 [英] getting data from database into class object array

查看:54
本文介绍了将数据从数据库获取到类对象数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我在实施计划时遇到一些问题。



在我的计划中是包含订单详细信息的数据库表名 sc_Order ..如orderid,itemid,地址......等



订单是我的班级



现在我必须从以下函数中的数据库表 sc_Order 中检索数据



Dear Friends,

I have some problem when implementing program.

In my program there is Database table name sc_Order containing order details.. like orderid,itemid,address...etc

Order is my class

now i have to retrieve data from database table sc_Order in the following function

public Order[] GetNewOrders()
     {
       string status=OrderStatus.Placed.ToString();
      Order[] orders = (from o in db.sc_Order where o.OrderStatus == status select o).ToArray<sc_Order>();

         return orders;
     }





现在我收到错误无法隐式将sc_Order转换为订单



所以请尽快给我解决方案。



感谢高级!!



now i am getting an error that "cannot implicitly convert sc_Order to Order"

so please give me solution as soon as possible.

Thanks in Advanced !!

推荐答案

我猜你使用的是实体框架作为数据访问技术。如果请确认你已经编写了如下的模型类,





I guess you are using entity framework as data accessing technique.if so please verify that you have written your model class as below,


using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;




[Table("sc_Order")]
    public class Order 
    {
        public int OrderId { get; set; }

        public string ItemId { get; set; }

        public string Address{ get; set; }
    }





如果您的数据库表名与您的模型类名不同,那么实体框架无法映射您的数据库。因此您必须使用注释在您的数据库中使用您的模型类名称指定更改的表名。



if your database table name is different from your model class name,then entity framework is unable to map with your database.So you have to use annotation for specifying the changed table name in your database with your model class name.


这篇关于将数据从数据库获取到类对象数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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