linq to entities检索数据库表行值并显示为label.Text C#.net

时间:2017-08-08 05:16:11

标签: c# asp.net linq linq-to-entities

使用LINQ to Entities。不是SQLConnections或任何其他方法。

我有一个网页,我需要在其中显示' ID'从一个DataTable和相应的名称'来自另一个DataTable,都在同一个数据库中。 ID和Name应显示为lblOrderId.Text = OrderId.ToString();和lblName.Text = Name.ToString();分别
不必是字符串,我只想要标签中数据库中的值。

现在看来,我没有收到任何标签文字,但是如果我使用底部的代码,我得到的就是订单和交货日期。

包含OrderId的数据表有两列; OrderId自动生成并位于第0列和ProductId。

包含Name的数据表有6列,Name在第1列,因为第0列包含自动生成的客户ID

我需要的是最后添加的名称和ID,以显示为标签。

我在过去5个小时内一直在这个网站和其他网站上进行研究,似乎无法找到适合我需求的解决方案。它是所有SQL连接以及向文本框稀疏地填充数据库值。

请参阅下面的代码,了解我所取得的成就,如果您需要任何进一步的编码或信息,请随时提出。

提前感谢您提供的任何建议或帮助。

ASPX.CS(后端)

protected void Page_Load(object sender, EventArgs e)
    {
        using (Entities myEntities = new Entities())
        {
             int OrderId = // Not sure what to put here, but if I don't have it I get the error *The name OrderId does not exist in the current context*
             var orderId = (from ord in myEntities.Orders
                           where ord.OrderId.Equals(OrderId) // Error here
                           select ord).Last();                

            lblOrderID.ForeColor = Color.Red;
            lblOrderID.Text = orderId.ToString();

使用以下有关名称的方法面对完全相同的错误。这两种方法都包含在相同的使用实体{}大括号中。

var name = (from ord in myEntities.Customer
                        where ord.Name.Equals(Name)
                        select ord).Last();

            lblName.ForeColor = Color.Red;
            lblName.Text = name.ToString();

关闭使用实体{}花括号,但仍然在Page_Load {}花括号内我有以下代码来添加订单日期和交货日期,如下所示

        DateTime date = DateTime.Now;
        DateTime DeliveryDate = date.AddDays(5);

        lblDate.ForeColor = Color.Red;
        lblDeliveryDate.ForeColor = Color.Red;

        lblDate.Text = date.ToLongDateString();
        lblDeliveryDate.Text = DeliveryDate.ToLongDateString();

完整的Page_Load

protected void Page_Load(object sender, EventArgs e)
    {
        using (Entities myEntities = new Entities())
        {
            //int OrderId = Convert.ToInt32(); // Not sure what to put here, but if I don't have it I get the error *The name OrderId does not exist in the current context*

            var orderId = (from ord in myEntities.Orders
                           where ord.OrderId.Equals(OrderId)// Error here
                           select ord).Last();

            lblOrderID.ForeColor = Color.Red;
            lblOrderID.Text = orderId.ToString();


            var name = (from ord in myEntities.Customer
                        where ord.Name.Equals(Name)
                        select ord).Last();

            lblName.ForeColor = Color.Red;
            lblName.Text = name.ToString();
        }

        DateTime date = DateTime.Now;
        DateTime DeliveryDate = date.AddDays(5);

        lblDate.ForeColor = Color.Red;
        lblDeliveryDate.ForeColor = Color.Red;

        lblDate.Text = date.ToLongDateString();
        lblDeliveryDate.Text = DeliveryDate.ToLongDateString();
    }

ASPX(前端)

    <p>
            Hi 
        <asp:Label ID="lblName" runat="server" Text=""></asp:Label>
            Thank you for shopping at Cineplex.<br />
            <br />

            Your Order ID number is: 
        <asp:Label ID="lblOrderID" runat="server" Text=""></asp:Label><br />
            <br />

            Your order was placed on:
        <asp:Label ID="lblDate" runat="server" Text=""></asp:Label><br />
            <br />

            Delivery date:
        <asp:Label ID="lblDeliveryDate" runat="server" Text=""></asp:Label>
        </p>

硬编码名称和订单ID并删除实体和查询,使用下面的代码在后端可以看到,这是一种享受,但这是不可接受的。成功购买后,客户需要查看订单ID 这里唯一的安慰是我在客户端可以阅读的所有四个标签中得到一个值。

 protected void Page_Load(object sender, EventArgs e)
    {
        lblName.ForeColor = Color.Red;
        lblName.Text = "User";

        lblOrderID.ForeColor = Color.Red;
        lblOrderID.Text = "1";

        DateTime date = DateTime.Now;
        DateTime DeliveryDate = date.AddDays(5);

        lblDate.ForeColor = Color.Red;
        lblDeliveryDate.ForeColor = Color.Red;

        lblDate.Text = date.ToLongDateString();
        lblDeliveryDate.Text = DeliveryDate.ToLongDateString();
    } 

2 个答案:

答案 0 :(得分:0)

您的问题是关于从数据库查询内容。您可以从查询中删除所有ASP内容,因为它只会分散您的问题。

相反,您应该向我们展示实体类(订单,客户)。据我了解,您希望Order.OrderIdCustomer.Name(订购它的客户)订购特定订单,但您不知道该订单的识别方式。

假设您使用通常的客户/订单样本模型,您的实体必须看起来像这样:

public class Customer
{
    public int CustomerId; // identifies a customer
    public string Name; // the customer's name
    public ICollection<Order> Orders; // collection of customer's orders
}

public class Order
{
    public int OrderId; // identifies an order
    public int CustomerId { get; } // ID of the customer who's order it is.
    public Customer Customer { get; } // The Customer entity of that customer
}

在Page_Load中(遗漏不相关的内容)

// 1st find out the ID of the one and only order the page is about
int orderId = 4711; // can't help here - where do you intend to get it from?

// 2nd retrieve from DB whatever we need later
string customerName;
using (var myEntities = new Entities())
{
    var orderInfo = (
        from order in myEntities.Orders
        where order.OrderId == orderId
        select new { CustomerName = order.Customer.Name }
        ).SingleOrDefault();
    if (orderInfo == null) // error handling if orderId is bad
        throw new Exception("No such order found.");
    customerName = orderInfo.CustomerName;
}

// 3rd use what we know to update controls
lblName.Text = customerName;

答案 1 :(得分:0)

结果。 我在aspx.cs页面上的两个不同的会话中保存了初始变量,它们是这样创建的:

int custID = Cust.CustomerId;
Session["CustID"] = Cust.CustomerId;

int newOrderID = ord.OrderId;
Session["newOrderID"] = ord.OrderId;

然后在aspx.cs页面上,我想查询数据库表,我做了以下内容:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CustID"] != null)
        {
            int CID = (int)Session["CustID"];

            using (var myEntities = new Entities())
            {
                var CustName = (from customer in myEntities.Customer
                                where customer.CustomerId == (CID)
                                select customer.Name).SingleOrDefault();
                if (CustName == null) // error handling if customer name does not exist
                    throw new Exception("No such Customer found.");
                lblName.ForeColor = Color.Red;
                lblName.Text = CustName;
            }
        }
        if (Session["newOrderID"] != null)
        {
            int OID = (int)Session["CustID"];

            using (var myEntities = new Entities())
            {
                var OrderNum = (from order in myEntities.Orders
                                where order.OrderId == (OID)
                                select order.OrderId).SingleOrDefault();
                if (OrderNum == 0) // error handling if customer name does not exist
                    throw new Exception("No such Order found.");
                lblOrderID.ForeColor = Color.Red;
                lblOrderID.Text = OrderNum.ToString();
            }
        }

        DateTime date = DateTime.Now;
        DateTime DeliveryDate = date.AddDays(5);

        lblDate.ForeColor = Color.Red;
        lblDeliveryDate.ForeColor = Color.Red;

        lblDate.Text = date.ToLongDateString();
        lblDeliveryDate.Text = DeliveryDate.ToLongDateString();
    }

感谢那些提出意见和建议的人。非常感激。