首页 > 开发 > 综合 > 正文

在C#中把两个DataTable连接起来,相当于Sql的Inner join方法

2024-07-21 02:26:52
字体:
来源:转载
供稿:网友

在下面的例子中实现了3个join方法,其目的是把两个datatable连接起来,相当于sql的inner join方法,返回datatable的所有列。
如果两个datatable中的datacolumn有重复的话,把第二个设置为columnname+"_second",下面是代码,希望对大家有所帮助。
using system;
using system.data;

namespace windowsapplication1
{
    public class sqlops
    {
        public sqlops()
        {           
        }

        public static datatable join (datatable first, datatable second, datacolumn[] fjc, datacolumn[] sjc)

        {

            //创建一个新的datatable

            datatable table = new datatable("join");


            // use a dataset to leverage datarelation

            using(dataset ds = new dataset())

            {

                //把datatable copy到dataset中

                ds.tables.addrange(new datatable[]{first.copy(),second.copy()});

                datacolumn[] parentcolumns = new datacolumn[fjc.length];

                for(int i = 0; i < parentcolumns.length; i++)

                {

                    parentcolumns[i] = ds.tables[0].columns[fjc[i].columnname];

                }

                datacolumn[] childcolumns = new datacolumn[sjc.length];

                for(int i = 0; i < childcolumns.length; i++)

                {

                    childcolumns[i] = ds.tables[1].columns[sjc[i].columnname];

                }


                //创建关联

                datarelation r = new datarelation(string.empty,parentcolumns,childcolumns,false);

                ds.relations.add(r);


                //为关联表创建列

                for(int i = 0; i < first.columns.count; i++)

                {

                    table.columns.add(first.columns[i].columnname, first.columns[i].datatype);

                }

                for(int i = 0; i < second.columns.count; i++)

                {

                    //看看有没有重复的列,如果有在第二个datatable的column的列明后加_second

                    if(!table.columns.contains(second.columns[i].columnname))

                        table.columns.add(second.columns[i].columnname, second.columns[i].datatype);

                    else

                        table.columns.add(second.columns[i].columnname + "_second", second.columns[i].datatype);

                }
              

                table.beginloaddata();

                foreach(datarow firstrow in ds.tables[0].rows)

                {

                    //得到行的数据

                    datarow[] childrows = firstrow.getchildrows(r);

                    if(childrows != null && childrows.length > 0)

                    {

                        object[] parentarray = firstrow.itemarray;

                        foreach(datarow secondrow in childrows)

                        {

                            object[] secondarray = secondrow.itemarray;

                            object[] joinarray = new object[parentarray.length+secondarray.length];

                            array.copy(parentarray,0,joinarray,0,parentarray.length);

                            array.copy(secondarray,0,joinarray,parentarray.length,secondarray.length);

                            table.loaddatarow(joinarray,true);

                        }

                    }

                }

                table.endloaddata();

            }


            return table;

        }


        public static datatable join (datatable first, datatable second, datacolumn fjc, datacolumn sjc)

        {

            return join(first, second, new datacolumn[]{fjc}, new datacolumn[]{sjc});

        }

        public static datatable join (datatable first, datatable second, string fjc, string sjc)

        {

            return join(first, second, new datacolumn[]{first.columns[fjc]}, new datacolumn[]{first.columns[sjc]});

        }


    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表