增加动态调用存储过程组件DSPDunamicSp

This commit is contained in:
falcon 2019-09-10 15:56:14 +08:00
parent 9eaae62a60
commit 73808e6fbf
10 changed files with 160 additions and 2 deletions

View File

@ -0,0 +1,13 @@
using System;
namespace Falcon.DynamicSP
{
/// <summary>
/// 无法获取存储过程返回结果架构异常
/// </summary>
[Serializable]
public class CanNotGetSchemaException:Exception
{
public CanNotGetSchemaException() : base("无法获取存储过程返回结果架构!") { }
}
}

View File

@ -0,0 +1,24 @@
using System;
namespace Falcon.DynamicSP
{
/// <summary>
/// 存储过程返回的一个数据
/// </summary>
public class DSPCell
{
/// <summary>
/// 列名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 存储过程列数据类型
/// </summary>
public Type ColumnType { get; set; }
/// <summary>
/// 存储过程返回值
/// </summary>
public object Value { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using System.Collections.ObjectModel;
namespace Falcon.DynamicSP
{
/// <summary>
/// 存储过程返回的数据表中的行
/// </summary>
public class DSPRow:Collection<DSPCell>
{
}
}

View File

@ -0,0 +1,11 @@
using System.Collections.ObjectModel;
namespace Falcon.DynamicSP
{
/// <summary>
/// 表示存储过程返回的数据表
/// </summary>
public class DSPTable:Collection<DSPRow>
{
}
}

View File

@ -0,0 +1,47 @@
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace Falcon.DynamicSP
{
public static class DatabaseFacadeExtend
{
/// <summary>
/// 执行动态存储过程。通过传入存储过程名称和参数获取返回结果
/// </summary>
/// <param name="dbf">数据库引用</param>
/// <param name="name">存储过程名称</param>
/// <param name="paras">传入存储过程的参数</param>
/// <returns>存储返回的数据结果</returns>
public static DSPTable RunDynamicSP(this DatabaseFacade dbf,string name,params SqlParameter[] paras) {
var table = new DSPTable();
var connection = dbf.GetDbConnection();
using(var cmd = connection.CreateCommand()) {
cmd.CommandText = name;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddRange(paras);
connection.Open();
var dr = cmd.ExecuteReader();
if(!dr.CanGetColumnSchema())
throw new CanNotGetSchemaException();
while(dr.Read()) {
var row = new DSPRow();
var columnSchema = dr.GetColumnSchema();
for(var i = 0;i < columnSchema.Count;i++) {
var cell = new DSPCell();
cell.Name = dr.GetName(i);
cell.Value = dr.IsDBNull(i) ? null : dr.GetValue(i);
cell.ColumnType = dr.GetFieldType(i);
row.Add(cell);
}
table.Add(row);
}
connection.Close();
return table;
}
}
}
}

View File

@ -0,0 +1,19 @@
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
namespace Falcon.DynamicSP
{
public static class DbContextExtend
{
/// <summary>
/// 对数据上下文调用存储过程,并获取返回数据
/// </summary>
/// <param name="db">数据库上下文</param>
/// <param name="name">存储过程名称</param>
/// <param name="paras">传入存储过程的参数</param>
/// <returns>存储返回的数据结果</returns>
public static DSPTable RunDynamicSP(this DbContext db,string name,params SqlParameter[] paras) {
return db.Database.RunDynamicSP(name,paras);
}
}
}

View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>动态调用存储过程并返回动态结果集</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Authors>Falcon</Authors>
<Copyright>Falcon</Copyright>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'NET461'">
<PackageReference Include="EntityFramework" Version="6.2.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,4 @@
动态获取存储过程返回结果方法。
调用`DatabaseFacade.RunDynamicSP`或`DbContext.RunDynamicSP`调用存储过程,传入存储过程名称和参数,可以获取一个`DSPTable`对象。
该对象包含存储过程返回的所有行对象DSPRow每一行对象包含一些列DSPCell对象。在列对象中存储值的相关信息。
对于某些特殊存储过程可能无法获取返回结果架构信息会抛出CanNotGetSchemaException异常。

View File

@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;NET461</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>

View File

@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.AutoTableLog", "Falc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.AutoTableLog.Test", "Falcon.AutoTableLog.Test\Falcon.AutoTableLog.Test.csproj", "{D60683D5-D6F6-41EE-9627-43C34C977C45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Falcon.ModelSP.Test", "Falcon.ModelSP.Test\Falcon.ModelSP.Test.csproj", "{C05DEBF9-79ED-4BA9-B8D5-CAF6F8EB4A87}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.ModelSP.Test", "Falcon.ModelSP.Test\Falcon.ModelSP.Test.csproj", "{C05DEBF9-79ED-4BA9-B8D5-CAF6F8EB4A87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Falcon.DynamicSP", "Falcon.DynamicSP\Falcon.DynamicSP.csproj", "{83C25A9A-84B9-4146-93B8-6415B7053CB2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -63,6 +65,10 @@ Global
{C05DEBF9-79ED-4BA9-B8D5-CAF6F8EB4A87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C05DEBF9-79ED-4BA9-B8D5-CAF6F8EB4A87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C05DEBF9-79ED-4BA9-B8D5-CAF6F8EB4A87}.Release|Any CPU.Build.0 = Release|Any CPU
{83C25A9A-84B9-4146-93B8-6415B7053CB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83C25A9A-84B9-4146-93B8-6415B7053CB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83C25A9A-84B9-4146-93B8-6415B7053CB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83C25A9A-84B9-4146-93B8-6415B7053CB2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE