Tag: datatable

如何编辑数据表中的行

我创建了一个数据表。 它有3列Product_id , Product_name和Product_price Datatable table= new DataTable(“Product”); table.Columns.Add(“Product_id”, typeof(int)); table.Columns.Add(“Product_name”, typeof(string)); table.Columns.Add(“Product_price”, typeof(string)); table.Rows.Add(1, “abc”, “100”); table.Rows.Add(2, “xyz”, “200”); 现在我想通过索引找到并更新该行。 比如说 我想将Product_name列的值更改为具有Product_id列值的“cde”:2。

比较两个DataTables的C#差异?

可能重复: C#,如何比较两个数据表A + B,如何显示B中但不在A中的行 我需要比较c#中的两个数据表来找出差异。 这两个数据表具有相同的模式。 最好的方法是什么? 可以使用linq查询完成吗? 如果是,怎么样?

如何在c#中将多个DataTable存储到单个DataSet中?

我有下面的代码,其中包含多个带有结果的DataTables,我需要传递一个包含所有DataTable值的DataSet。 MySqlCommand cmd = new MySqlCommand(getLikeInfo, con); MySqlDataAdapter da = new MySqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con); MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1); DataTable dt1 = new DataTable(); da1.Fill(dt1); MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con); MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2); DataTable dt2 = new DataTable(); da2.Fill(dt2); MySqlCommand cmd3 = […]

为什么从WCF服务返回数据集或数据表不是一个好习惯? 有什么替代品?

我正在使用大学管理系统,我正在使用WCF服务,在服务中我使用DataTables和DataSets从数据库和数据库获取数据是sql server。 我的问题是 使用DataTables和数据集“良好实践”还是“不良实践”? 如果不好,DataTable / DataSet的替代方案是什么? 如果不好,主要原因是什么?

从两个数据表中查找公共列,并将其用于LINQ中的Join条件

我有两个数据表,这些是完全动态的。 这些将在运行时生成。 现在我想通过查找公共列来加入这些表。 请查看以下代码以获取更多信息 public DataTable DataTableJoiner(DataTable dt1, DataTable dt2) { using (DataTable targetTable = dt1.Clone()) { var dt2Query = dt2.Columns.OfType().Select(dc => new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping)); var dt2FilterQuery = from dc in dt2Query.AsEnumerable() where targetTable.Columns.Contains(dc.ColumnName) == false select dc; targetTable.Columns.AddRange(dt2FilterQuery.ToArray()); var rowData=from row1 in dt1.AsEnumerable() join row2 in dt2.AsEnumerable() on row1.Field(“ID”) equals row2.Field(“ID”) select […]

将DataTable转换为CSV的最有效方法

我正在使用DataTable,我需要将它们转换为CSV文件格式。 我正在使用的大多数表都有超过50,000条记录,因此我试图最小化转换它们所需的时间。 这是我目前的方法: public static string table_to_csv(DataTable table) { string file = “”; foreach (DataColumn col in table.Columns) file = string.Concat(file, col.ColumnName, “,”); file = file.Remove(file.LastIndexOf(‘,’), 1); file = string.Concat(file, “\r\n”); foreach (DataRow row in table.Rows) { foreach (object item in row.ItemArray) file = string.Concat(file, item.ToString(), “,”); file = file.Remove(file.LastIndexOf(‘,’), 1); file = string.Concat(file, “\r\n”); […]

如何计算LINQ(到数据集)中DataTable列的总和?

我刚刚开始阅读LINQ,我想开始将它合并到我的代码中。 我知道如何通过“Foreach” – 通过行或通过在特定列上执行compute.sum来计算DataTable列的总和。 如何使用LINQ to DataSet进行等效操作?

LINQ TO DataSet:数据表上的多个分组

我正在使用Linq数据集来查询数据表。 如果我想在数据表上的“Column1”上执行一个组,我使用以下查询 var groupQuery = from table in MyTable.AsEnumerable() group table by table[“Column1”] into groupedTable select new { x = groupedTable.Key, y = groupedTable.Count() } 现在我想在两列“Coulmn1”和“Column2”上执行group by。 任何人都可以告诉我语法或者在数据表中提供一个解释多个组的链接吗? 谢谢

在具有特定id的数据表中查找行

我在数据表中有两列: ID, Calls. 如何where ID = 5找到Calls的值? 5可以是任何数字,例如它。 每行都有一个唯一的ID。

如何使用LINQ进行分组并按特定列排序

更新 :添加了一个新的列,将日期和时间列组合成字符串字段到DateTimeCombined列,这是一个DateTime字段 那么LINQ应该做的是按名称列分组,并为每个具有最早日期+时间的名称获取行。 然后它应该为名称添加行的其余部分。 DataTable init: dataT = new DataTable(); dataT.Columns.Add(“Date”, typeof(string)); dataT.Columns.Add(“Time”, typeof(string)); dataT.Columns.Add(“Day”, typeof(string)); dataT.Columns.Add(“Name”, typeof(string)); dataT.Columns.Add(“Place”, typeof(string)); dataT.Columns.Add(“DateTimeCombined”, typeof(DateTime)); dataT.Columns.Add(“NameMessage”, typeof(string)); 所以这是起始DataTable(默认检索): Date Time Day Name Place DateTimeCombined NameMessage 6/29/2017 8:30AM MON John Orlance 6/29/2017 8:30:00 AM 6/29/2017 8:40AM MON John Orlance 6/29/2017 8:40:00 AM 6/29/2017 8:50AM MON John Orlance 6/29/2017 8:50:00 […]