C#返回链接到productlist信息表的表的通用列表 [英] C# returning generic list of table linked to productlist info table

查看:96
本文介绍了C#返回链接到productlist信息表的表的通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个数据库表来存储我的收据信息,在我的收据上我有收据上的产品清单。为了存储我读过的列表,我需要制作另一个列表中的列表数据并将收据和我的产品列表链接在一起,我应该与receiptti链接。要检索productlist表我在表中有收据ID但在我的产品类中我没有收据ID我是否有一个收据对象和产品对象以及它们在1个新列表< t>中的两个类的属性。我想返回该列表的新类< t>得到我的数据。

我创建了一个新类,并在其中有一个收据对象和产品对象,以返回两者的属性这是我应该这样做的正确方法吗?任何信息/帮助/提示将不胜感激。





Hello everyone, I have a database table to store my receipt info and on my receipt I have a list of products on the receipt. To store a list I read I need to make another table of that list data and to link the receipt and list of my products together I’m supposed to link with receiptid. To retrieve the productlist table I have receipt id in the table but in my product class I do not have receipt id do I have a receipt object and product object and properties off both of them classes in 1 new List<t> I wanted to return a new class of that list<t> do get my data.
I made a new class and have a receipt object and product object in it to return properties of both is this the correct way I should be doing this? Any info / help / tips would be greatly appreciated.


//I am still in the processing of finishing this method
        public static List<ReturnReceiptProductList> GetReceiptProductList(int rid)
        {
            List<ReturnReceiptProductList> productList = new List<ReturnReceiptProductList>();
            ReturnReceiptProductList returnReceipt = new ReturnReceiptProductList();
            using (SqlConnection connection = ConnectionString.GetSqlConnection())
            {
                string sqlList = "SELECT * FROM OrdersList WHERE ReceiptId = @rid";
                using (SqlCommand cmdList = new SqlCommand(sqlList, connection))
                {
                    cmdList.Parameters.AddWithValue("@rid", rid);
                    connection.Open();
                    using (SqlDataReader reader = cmdList.ExecuteReader())
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                Product product = new Product();
                                Receipt receipt = new Receipt();
                                receipt.ReceiptID = Convert.ToInt32(reader["ReceiptId"].ToString());
                                receipt.UserId = Convert.ToInt32(reader["UserId"].ToString());
                                product.ProductId = Convert.ToInt32(reader["ProductId"].ToString());
                                product.ProductName = reader["ProductName"].ToString();
                                product.ProductQuantity = Convert.ToInt32(reader["ProductQuantity"].ToString());
                                product.ProductPrice = Convert.ToDecimal(reader["ProductPrice"].ToString());
                                product.ProductTax = Convert.ToDecimal(reader["ProductTax"].ToString());
                            }
                        }
                    }
                }
            }
        }
    public class ReturnReceiptProductList
    {
        //Returning the values to link Product & Receipt.

        public Product Product { get; set; }

        public Receipt Receipt { get; set; }

        public ReturnReceiptProductList() { }

        public ReturnReceiptProductList(Product p, Receipt r)
        {
            Product = p;
            Receipt = r;
        }
    }





我的尝试:



将表链接到列表中

和许多其他方式



What I have tried:

linking a table to a table of a list
and many other ways

推荐答案

你也可以使用 LINQ ,这需要一些研究,但提供了很多功能和灵活性。请参阅此处的示例: C#中简单的LINQ to SQL [ ^ ]

这里:使用LINQ查询SQL Server数据库到SQL [ ^ ]
You can also use LINQ, this will take some study but provides a lot of power and flexibility. See example here: Simple LINQ to SQL in C#[^]
And here: Querying SQL Server databases using LINQ to SQL[^]


这篇关于C#返回链接到productlist信息表的表的通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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