我有一个函数来获取数据库中的数据。 以下是代码。
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"] }); }更多推荐
发布评论