简体   繁体   English

订购Northwind Access数据库的对象

[英]Order objects for Northwind Access database

I need to build two objects: an OrderList and an Order. 我需要构建两个对象:OrderList和Order。

Using those two objects, I have to populate a DataGridView with a history of the orders. 使用这两个对象,我必须使用订单历史记录填充DataGridView。 However, I am instructed not to use binding sources for the connection or other drag and drop controls. 但是,指示我不要将绑定源用于连接或其他拖放控件。 Unfortunately, from Google it seems like those are the most popular options for this type of problem. 不幸的是,从Google看来,这些是这类问题的最受欢迎选项。

Can anyone point me in the right direction? 谁能指出我正确的方向? I don't have much experience with C#. 我对C#没有太多经验。

Thanks. 谢谢。

You could to create an Order class: 您可以创建一个Order类:

public class Order
{
    public int OrderID { get; set; }
    public DateTime OrderDate { get; set; }
}

Read your database records and load your orders collection 阅读数据库记录并加载订单集合

List<Order> orders = new List<Order>();
using(SqlConnection cn = new SqlConnection("..."))
using (SqlCommand cm = cn.CreateCommand())
{
    cn.Open();
    cm.CommandText = "SELECT OrderId, OrderDate FROM Orders";
    SqlDataReader dr = cm.ExecuteReader();
    while (dr.Read())
    {
        orders.Add(new Order()
        {
            OrderId = dr.GetInt32(dr.GetOrdinal("OrderId")),
            OrderDate = dr.GetDateTime(dr.GetOrdinal("OrderDate"))
        });
    }
}

About GridView part, you should take a look into ASP.NET data binding overview 关于GridView部分,您应该看一下ASP.NET数据绑定概述

This is actually a fairly trivial problem. 这实际上是一个相当琐碎的问题。 You will need to use these things: 您将需要使用以下内容:

Once you have a dataadapter and its associated dataset, you can simply loop to fill the grid. 一旦有了数据适配器及其关联的数据集,就可以简单地循环以填充网格。

您可以绑定到任何实现IEnumerable的数据集合,因为当您在网格上调用DataBind()函数时,它将遍历数据并将网格通过反射(使用反射)绑定到集合中存储的每个对象的属性。对象并寻找与为网格中每个绑定字段指定的DataField匹配的属性。

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

相关问题 无法从Visual Studio访问Northwind数据库 - Can't access Northwind database from Visual Studio 罗斯文(Northwind)数据库Linq查询 - Northwind Database Linq Query 在 ASP.NET 和实体框架中使用 NorthWind 数据库 - Working with NorthWind database in ASP.NET and Entity Framework 我可以将对象添加到Access数据库中吗? - Can I add objects to my Access database? 为什么根据对数据库的调用顺序来更快地访问数据库? - Why access to the database is faster depending on order of calls to database? Visual C#SQL Server罗斯文数据库错误:无效的对象名称&#39;dbo.Products&#39; - Visual C# SQL Server Northwind Database Error: Invalid Object Name 'dbo.Products' C#实体数据模型中连接到本地罗斯文数据库的权限和超时问题 - permission and timeout issues connecting to local northwind database in C# entity data model 尝试连接到Access数据库表以检索数据,但是遇到困难 - Trying to connect to a Access database table in order to retrieve data, but encounter difficulties 如何按日期按降序显示listview中的访问数据库数据 - How to Display access database data in listview by date in descending order 是否存在一种对象,可以将Buttons和MenuItem对象都强制转换为对象,以访问其Tag属性? - Is there a type of object, to which I can cast both Buttons AND MenuItem objects in order to access their Tag properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM