在C#中把两个DataTable连接起来

本文介绍了如何在C#中使用3种不同的Join方法将两个DataTable进行连接,类似于SQL的Inner Join操作。通过创建DataRelation并添加到DataSet,然后遍历行数据,将结果加载到新的DataTable中,处理可能存在的重复列名。提供的代码示例展示了具体的实现过程。
摘要由CSDN通过智能技术生成

在下面的例子中实现了3个Join方法,其目的是把两个DataTable连接起来,相当于Sql的Inner Join方法,返回DataTable的所有列。
如果两个DataTable中的DataColumn有重复的话,把第二个设置为ColumnName+"_Second",下面是代码,希望对大家有所帮助。
using System;
using System.Data;

namespace 视窗系统Application1
{
    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()});

                DataCo

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 三层架构,实现两个数据表的连接通常可以采用 SQL 语句JOIN 操作,或者使用 LINQ 进行数据集合的关联查询。以下是两种方法的示例代码: 1. 使用 SQL JOIN 操作实现数据表连接 ```csharp using System.Data; using System.Data.SqlClient; // 数据库连接字符串 string connectionString = "Server=数据库服务器地址;Database=数据库名称;User Id=用户名;Password=密码;"; // SQL查询语句,使用 LEFT JOIN 操作实现两个表的连接 string sql = "SELECT * FROM 表1 LEFT JOIN 表2 ON 表1.字段1 = 表2.字段2"; // 创建数据库连接对象 using (SqlConnection connection = new SqlConnection(connectionString)) { // 创建SqlDataAdapter对象 SqlDataAdapter adapter = new SqlDataAdapter(sql, connection); // 创建数据集对象 DataSet dataset = new DataSet(); // 使用SqlDataAdapter对象填充数据集 adapter.Fill(dataset, "表1"); // 获取数据表 DataTable table = dataset.Tables["表1"]; // 处理数据表 // ... } ``` 2. 使用 LINQ 进行数据集合的关联查询 ```csharp using System.Collections.Generic; using System.Linq; // 定义数据表实体类 public class Table1 { public int Id { get; set; } public string Name { get; set; } } public class Table2 { public int Id { get; set; } public int Table1Id { get; set; } public string Description { get; set; } } // 定义数据访问层接口和实现类 public interface IDataAccess { List<Table1> GetTable1List(); List<Table2> GetTable2List(); } public class DataAccess : IDataAccess { public List<Table1> GetTable1List() { // 获取 Table1 数据表的数据集合 // ... } public List<Table2> GetTable2List() { // 获取 Table2 数据表的数据集合 // ... } } // 在业务逻辑层使用 LINQ 进行数据集合的关联查询 public class BusinessLogic { private IDataAccess dataAccess; public BusinessLogic(IDataAccess dataAccess) { this.dataAccess = dataAccess; } public List<Table1> GetJoinedTableList() { // 获取 Table1 和 Table2 的数据集合 List<Table1> table1List = dataAccess.GetTable1List(); List<Table2> table2List = dataAccess.GetTable2List(); // 使用 LINQ 进行数据集合的关联查询 var joinedTableList = from t1 in table1List join t2 in table2List on t1.Id equals t2.Table1Id into temp from t2 in temp.DefaultIfEmpty() select new { Table1Id = t1.Id, Table1Name = t1.Name, Table2Description = t2 != null ? t2.Description : string.Empty }; // 转换为 Table1 对象的集合并返回 return joinedTableList.Select(j => new Table1() { Id = j.Table1Id, Name = j.Table1Name, Description = j.Table2Description }).ToList(); } } ``` 以上是两种实现两个数据表连接的示例代码,具体实现方法需要根据实际需求进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值