如何将新列添加到数据集 [英] How to Add New column to dataset

查看:100
本文介绍了如何将新列添加到数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我从数据库中获取数据并分配给数据集,我想再向该数据集添加两列,我该怎么做呢。我尝试了以下它不工作



Hi all,

I am fetching data from a database and assigned to a dataset and i want to add two more columns to that dataset how can i do that . I tried the following its not working

DataSet dsACC = new DataSet();
dsACC = myEmployee.GetTimesheetsampleTable();





dsACC数据集包含empid和名称的数据





dsACC dataset contains data of empid and name

DataSet dsTimesheet = new DataSet();
tableColumns.Add(dsTimesheet.Tables[1].Columns.Add("Date", typeof(string)));
tableColumns.Add(dsTimesheet.Tables[1].Columns.Add("FromTime", typeof(string)));
tableColumns.Add(dsTimesheet.Tables[1].Columns.Add("ToTime", typeof(string)));
tableColumns.Add(dsTimesheet.Tables[1].Columns.Add("Note", typeof(string)));





我使用



i used

dsACC.Merge(dsTimesheet);





但不合并桌子。



我需要这样的数据集





but it does not merge the table.

I need a dataset like this

Empid   Name   Date   FromTime   ToTime    Note 
  1     Hari 

  2     Viji  





谢谢

parithi



thanks
parithi

推荐答案

首先,你不能将数据列添加到数据集中,但是对于数据表而言,我明白你的意思。

使用这些东西比人们想象的要多一些,为了你的目的,我不会我认为你需要合并功能,在这里做了一个脚手架样本:



Well first of all you cannot add columns to dataset, but to datatables but i get your point.
Working with these things is a bit more manual than one might think and for your purpose i don't think you need merge functionality, made a scaffolding sample here:

static void Main(string[] args)
       {
           var ds = GetDataset();
           var tbl = ds.Tables[0];
           tbl.Columns.AddRange(new []{ new DataColumn("Third"), new DataColumn("Fourth") });

           foreach(DataColumn clmn in tbl.Columns){
                   Console.WriteLine(clmn.ColumnName);
           }
           Console.WriteLine("\r\nHit any key to end");
           Console.ReadKey();
       }

       private static DataSet GetDataset()
       {
           var ds = new DataSet();
           var tbl = new DataTable();
           tbl.Columns.Add(new DataColumn("First"));
           tbl.Columns.Add(new DataColumn("Second"));
           ds.Tables.Add(tbl);
           return ds;
       }


您无法按照尝试的方式合并数据表。



请阅读此 DataTable .Merge方法 [ ^ ]找出为什么不可能。主要原因是:两个DataTable都必须具有大致相似的模式!



而不是创建新数据集,将列添加到现有数据表中

You can't merge datatables the way you're trying to do it.

Please, read this DataTable.Merge method[^] to find out why it's impossible. The main reason is: both DataTables must have largely similar schemas!

Rather than creating new dataset, add columns into existing datatable
dtAcc = dsAcc.Tables[1];
dtAcc.Columns.Add("Date", typeof(string));
dtAcc.Columns.Add("FromTime", typeof(string));
dtAcc.Columns.Add("ToTime", typeof(string));
dtAcc.Columns.Add("Note", typeof(string));





详情请见:将列添加到DataTable [ ^ ]


这篇关于如何将新列添加到数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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