两种获取connectionString的方式

public static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;



<connectionStrings>
  <add name="ConnectionString" connectionString="Data Source=localhost;Persist Security Info=True;User ID=***;Password=***;Unicode=True"
   providerName="System.Data.OracleClient" />
</connectionStrings>

 

public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

<add key="ConnectionString" value="server=localhost;database=***;uid=sa;pwd=***"></add>



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值