结果集存储在模型上

编程入门 行业动态 更新时间:2024-10-08 18:39:54
结果集存储在模型上 - C#.NET 2.0(Result Set Stored on Models - C# .NET 2.0)

我有一个函数来获取数据库中的数据。 以下是代码。

public DataTable getAllTransaction(OleDbConnection conn) { OleDbDataAdapter oleAdapter = new OleDbDataAdapter(); string query = ""; DataTable tblResult = new DataTable(); query = @"SELECT t.id AS `Transaction ID`, c.id AS `Client ID`, c.clientname AS `Client Name`, t.cashvalue AS `Cash Value`, t.amount AS `Amount`, t.transdate AS `Transaction Date`, t.remarks AS `Remarks` FROM client AS c INNER JOIN `transaction` AS t ON c.id=t.clientid"; oleAdapter.SelectCommand = new OleDbCommand(query, conn); oleAdapter.Fill(tblResult); return tblResult; }

我的问题是,如何将结果集存储到模型中(例如,我不想返回DataTable)。 下面是我的模型类。

Class TransactionModel { public int transID { get; set; } public int clientID { get; set; } public string clientName { get; set; } public double cashValue { get; set; } public double amout { get; set; } public DateTime transDate { get; set; } public string remarks { get; set; } }

I have a function that gets the data in the database. Below are the code.

public DataTable getAllTransaction(OleDbConnection conn) { OleDbDataAdapter oleAdapter = new OleDbDataAdapter(); string query = ""; DataTable tblResult = new DataTable(); query = @"SELECT t.id AS `Transaction ID`, c.id AS `Client ID`, c.clientname AS `Client Name`, t.cashvalue AS `Cash Value`, t.amount AS `Amount`, t.transdate AS `Transaction Date`, t.remarks AS `Remarks` FROM client AS c INNER JOIN `transaction` AS t ON c.id=t.clientid"; oleAdapter.SelectCommand = new OleDbCommand(query, conn); oleAdapter.Fill(tblResult); return tblResult; }

My problem is, how could I store the result set into model (e.g. I don't want to return DataTable). Below is my Model Class.

Class TransactionModel { public int transID { get; set; } public int clientID { get; set; } public string clientName { get; set; } public double cashValue { get; set; } public double amout { get; set; } public DateTime transDate { get; set; } public string remarks { get; set; } }

最满意答案

您可以使用LINQ并执行:

var tranModel = from r in tblResult.Tables[0] select new TransactionModel { transId = r.Field<int>("transID"), clientId = r.Field<int>("clientId"), clientName = r.Field<string>("ClientName") }

请注意,因为您使用的是.NET 2.0. LINQ不是直接可用的。 您将不得不使用像LINQBridge这样的LINQBridge : http : //www.albahari.com/nutshell/linqbridge.aspx

另一种方法是循环遍历tblResult所有行,并具有TransactionModel的通用列表。 例如:

List<TransactionModel> tModels = new List<TransactionModel>(); foreach (var row in tblResult.Tables[0].Rows) { tModels.Add(new TransactionModel { transId = row["TransId"], clientId = row["ClientId"], clientName = row["clientName"] }); }

You could use LINQ and do:

var tranModel = from r in tblResult.Tables[0] select new TransactionModel { transId = r.Field<int>("transID"), clientId = r.Field<int>("clientId"), clientName = r.Field<string>("ClientName") }

Note since you are using .NET 2.0. LINQ is not directly available. You will have to use Something like LINQBridge: http://www.albahari.com/nutshell/linqbridge.aspx

Another alternative is to loop through all of the rows in tblResult and have a generic list of TransactionModel. For instance:

List<TransactionModel> tModels = new List<TransactionModel>(); foreach (var row in tblResult.Tables[0].Rows) { tModels.Add(new TransactionModel { transId = row["TransId"], clientId = row["ClientId"], clientName = row["clientName"] }); }

更多推荐

本文发布于:2023-08-06 11:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1449364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!