Skip to content

Commit

Permalink
data
Browse files Browse the repository at this point in the history
  • Loading branch information
yibei333 committed Aug 13, 2024
1 parent 2bfcbfc commit 9747613
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion assets/sharpdevlib.data.version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0.5-alpha
2.0.0.6-alpha
2 changes: 1 addition & 1 deletion src/SharpDevLib.Data/SharpDevLib.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>2.0.0.5-alpha</Version>
<Version>2.0.0.6-alpha</Version>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
37 changes: 37 additions & 0 deletions src/SharpDevLib.Data/SqlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,43 @@ public async Task<DataSet> ExecuteDataSetAsync(string sql, DbParameter[] paramet
return await Task.Run(() => ExecuteDataSet(sql, parameters), cancellationToken);
}

/// <summary>
/// 检索数据表格
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">sql参数</param>
/// <returns>数据表格</returns>
public DataTable ExecuteDataTable(string sql, params DbParameter[] parameters)
{
var set = ExecuteDataSet(sql, parameters);
var table = set.Tables[0];
set.Tables.Remove(table);
return table;
}

/// <summary>
/// 检索数据表格
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">sql参数</param>
/// <returns>数据表格</returns>
public async Task<DataTable> ExecuteDataTableAsync(string sql, params DbParameter[] parameters)
{
return await Task.Run(() => ExecuteDataTable(sql, parameters));
}

/// <summary>
/// 检索数据表格
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="parameters">sql参数</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <returns>数据表格</returns>
public async Task<DataTable> ExecuteDataTableAsync(string sql, DbParameter[] parameters, CancellationToken cancellationToken)
{
return await Task.Run(() => ExecuteDataTable(sql, parameters), cancellationToken);
}

/// <summary>
/// 执行非查询sql语句
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/SharpDevLib.Tests/Data/SqlHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,6 +112,23 @@ public async Task ExecuteDataSetAsyncTest()
Assert.AreEqual(UserFavorites.Serialize(), dataSet.Tables[1].ToList<UserFavorite>().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()
{
Expand Down

0 comments on commit 9747613

Please sign in to comment.