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

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

问题描述

for (int i = 0; i < 5; i++)
            {
                DataTable tbl, tbl2;
                SqlDataAdapter adp = new SqlDataAdapter("Select ID, user ,Date, vehicle from booking where ID=''100'' and user=''james''", con);
                tbl = new DataTable();
                adp.Fill(tbl);
            }



在tbl datatable中,每次都会生成以下数据.即
身份用户日期车辆
100詹姆斯2012年2月2日宝马

这些以上值将生成5次,我想将所有5条记录保存在另一个数据表中,即tbl2.



In tbl datatable the following data is generating every time. ie
ID user Date vehicle
100 james 02-02-2012 BMW

These above value will generate 5 times and i want to save all 5 records in another datatable ie tbl2.

推荐答案

INSERT INTO new_db.dbo.TableA
SELECT * FROM old_db.dbo.TableB


SqlDataAdapter da1 = new SqlDataAdapter("Select booking_id,user_id,convert(char,date_of_travel,103) as date_of_travel, no_of_vehicle,vehicle from booking where user_id=''" + Session["a"] + "'' and booking_id=''" + Session["key"].ToString() + "''", con);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        if (dt1.Rows.Count > 0)
        {
            int loop;
            DataTable tbl;
            DataTable tbl2 = new DataTable();
            DataSet ds = new DataSet();
            ds.Tables.Add(new DataTable());
            ds.Tables[0].Columns.Add("ID", typeof(int));
            ds.Tables[0].Columns.Add("Tour_Code", typeof(int));
            ds.Tables[0].Columns.Add("Tour_Date", typeof(string));
            //ds.Tables[0].Columns.Add("No_Of_Vehicle", typeof(int));
            ds.Tables[0].Columns.Add("Vehicle", typeof(string));
            tbl2 = ds.Tables[0].Clone();
            loop = Convert.ToInt32(dt1.Rows[0][3].ToString());
            for (int i = 0; i < loop; i++)
            {
                SqlDataAdapter adp = new SqlDataAdapter("Select booking_id as ID,user_id as Tour_Code,convert(char,date_of_travel,103) as Tour_Date, vehicle as Vehicle from booking where user_id=''" + Session["a"] + "'' and booking_id=''" + Session["key"].ToString() + "''", con);
                tbl = new DataTable();
                adp.Fill(tbl);

                foreach (DataRow dr in tbl.Rows)
                {
                    DataRow newRow = tbl2.NewRow();
                    newRow.ItemArray = dr.ItemArray;
                    tbl2.Rows.Add(newRow);
                }

            }
            grdveh.DataSource = tbl2;
            grdveh.DataBind();
        }


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

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