From 9747613cdbb6f92edf5735a91e3dc76b7cb06718 Mon Sep 17 00:00:00 2001 From: yibei333 Date: Tue, 13 Aug 2024 10:13:20 +0800 Subject: [PATCH] data --- assets/sharpdevlib.data.version.txt | 2 +- src/SharpDevLib.Data/SharpDevLib.Data.csproj | 2 +- src/SharpDevLib.Data/SqlHelper.cs | 37 ++++++++++++++++++++ src/SharpDevLib.Tests/Data/SqlHelperTests.cs | 18 ++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/assets/sharpdevlib.data.version.txt b/assets/sharpdevlib.data.version.txt index 8025f93..da5c67d 100644 --- a/assets/sharpdevlib.data.version.txt +++ b/assets/sharpdevlib.data.version.txt @@ -1 +1 @@ -2.0.0.5-alpha \ No newline at end of file +2.0.0.6-alpha \ No newline at end of file diff --git a/src/SharpDevLib.Data/SharpDevLib.Data.csproj b/src/SharpDevLib.Data/SharpDevLib.Data.csproj index fabcb81..e02b7c0 100644 --- a/src/SharpDevLib.Data/SharpDevLib.Data.csproj +++ b/src/SharpDevLib.Data/SharpDevLib.Data.csproj @@ -17,7 +17,7 @@ MIT True True - 2.0.0.5-alpha + 2.0.0.6-alpha False README.md true diff --git a/src/SharpDevLib.Data/SqlHelper.cs b/src/SharpDevLib.Data/SqlHelper.cs index 10f6c2b..313b680 100644 --- a/src/SharpDevLib.Data/SqlHelper.cs +++ b/src/SharpDevLib.Data/SqlHelper.cs @@ -217,6 +217,43 @@ public async Task ExecuteDataSetAsync(string sql, DbParameter[] paramet return await Task.Run(() => ExecuteDataSet(sql, parameters), cancellationToken); } + /// + /// 检索数据表格 + /// + /// sql语句 + /// sql参数 + /// 数据表格 + public DataTable ExecuteDataTable(string sql, params DbParameter[] parameters) + { + var set = ExecuteDataSet(sql, parameters); + var table = set.Tables[0]; + set.Tables.Remove(table); + return table; + } + + /// + /// 检索数据表格 + /// + /// sql语句 + /// sql参数 + /// 数据表格 + public async Task ExecuteDataTableAsync(string sql, params DbParameter[] parameters) + { + return await Task.Run(() => ExecuteDataTable(sql, parameters)); + } + + /// + /// 检索数据表格 + /// + /// sql语句 + /// sql参数 + /// CancellationToken + /// 数据表格 + public async Task ExecuteDataTableAsync(string sql, DbParameter[] parameters, CancellationToken cancellationToken) + { + return await Task.Run(() => ExecuteDataTable(sql, parameters), cancellationToken); + } + /// /// 执行非查询sql语句 /// diff --git a/src/SharpDevLib.Tests/Data/SqlHelperTests.cs b/src/SharpDevLib.Tests/Data/SqlHelperTests.cs index 80b9ff8..3baabd1 100644 --- a/src/SharpDevLib.Tests/Data/SqlHelperTests.cs +++ b/src/SharpDevLib.Tests/Data/SqlHelperTests.cs @@ -7,6 +7,7 @@ using SharpDevLib.Tests.TestData.Data; using System; using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Runtime.CompilerServices; @@ -111,6 +112,23 @@ public async Task ExecuteDataSetAsyncTest() Assert.AreEqual(UserFavorites.Serialize(), dataSet.Tables[1].ToList().Serialize()); } + [TestMethod] + public async Task ExecuteDataTableAsyncTest() + { + SqlHelper.Config(SqliteFactory.Instance, SourceConnectionString); + using var sqlHelper = new SqlHelper(); + + var sql = $@" + SELECT [Name],Age FROM [User]; + SELECT [Name],Favorite FROM [UserFavorite]; + SELECT a.[Name],a.Age,b.Favorite FROM [User] a INNER JOIN [UserFavorite] b ON a.Name=b.Name; + "; + var table = await sqlHelper.ExecuteDataTableAsync(sql); + table.TableName = "xx"; + var set = new DataSet(); + set.Tables.Add(table); + } + [TestMethod] public void ExecuteNoneQueryTest() {