如何将一个数据表复制到另一个 [英] How to copy one datatable to another

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

问题描述

我将值从数据库存储到数据表中.
我的数据表有以下

I am stored values to datatable from database.
My datatable has following

jc_id  jc_name jc_car        jc_color  jc_plate jc_service  jc_spare jc_qty
1	asdfsa	Maruti800	red	123	NULL	    NULL	NULL
NULL	NULL	NULL		NULL	NULL	WaterWash   NULL	NULL
NULL    NULL    NULL            NULL    NULL    WaterWash   NULL         NULL
NULL    NULL    NULL            NULL    NULL    NULL        Star Wars   34.00
NULL    NULL    NULL            NULL    NULL    NULL        Shrek   43.00




在此数据表中,我想要没有空值的jc_service列
喜欢




In this datatable I want a jc_service column without null value
like

jc_service
WaterWash
WaterWash



我要将此列仅复制到另一个数据表中怎么办?
请帮助我...



I want to this copy this column only to another datatable how to do it?
Please help me...

推荐答案

您可以将此代码用于您的任务.

You may use this code for your task.

DataTable dtTarget = new DataTable();
    dtTarget = dtSource.Clone();
    DataRow[] rowsToCopy;
    rowsToCopy = dtSource.Select("key='" + matchString + "'");
    foreach (DataRow temp in rowsToCopy)
    {
        dtTarget.ImportRow(temp);
    }


devusha,

好吧,在这种情况下,您可以手动添加一列,例如:

Hi devusha,

Well, in this case you add a column manually like:

dt1.Columns.Add("newColumnName",Type.GetType("String"));



并且如果您想将此列复制到另一个数据表,则可以执行以下操作:



and If you want to copy this column to another datatable, you can do like this:

DataTable dt2 = new DataTable();
dt2.Columns.Add(dt1.Columns["newColumnName"]);



希望这会有所帮助!


快乐编码:)

最好的问候,
Sunny K



Hope this helps!


Happy Coding:)

Best Regards,
Sunny K


您好devausha,您不想要 joins 吗?
我认为这可能对您有帮助
Hi devausha, What dont you look for joins?
I think it may help you


这篇关于如何将一个数据表复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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