diff --git a/Falcon.DynamicSP/CanNotGetSchemaException.cs b/Falcon.DynamicSP/CanNotGetSchemaException.cs new file mode 100644 index 0000000..4de118c --- /dev/null +++ b/Falcon.DynamicSP/CanNotGetSchemaException.cs @@ -0,0 +1,13 @@ +using System; + +namespace Falcon.DynamicSP +{ + /// + /// 无法获取存储过程返回结果架构异常 + /// + [Serializable] + public class CanNotGetSchemaException:Exception + { + public CanNotGetSchemaException() : base("无法获取存储过程返回结果架构!") { } + } +} diff --git a/Falcon.DynamicSP/DSPCell.cs b/Falcon.DynamicSP/DSPCell.cs new file mode 100644 index 0000000..3086b1f --- /dev/null +++ b/Falcon.DynamicSP/DSPCell.cs @@ -0,0 +1,24 @@ +using System; + +namespace Falcon.DynamicSP +{ + /// + /// 存储过程返回的一个数据 + /// + public class DSPCell + { + /// + /// 列名称 + /// + public string Name { get; set; } + /// + /// 存储过程列数据类型 + /// + public Type ColumnType { get; set; } + /// + /// 存储过程返回值 + /// + public object Value { get; set; } + } + +} diff --git a/Falcon.DynamicSP/DSPRow.cs b/Falcon.DynamicSP/DSPRow.cs new file mode 100644 index 0000000..bdd6288 --- /dev/null +++ b/Falcon.DynamicSP/DSPRow.cs @@ -0,0 +1,11 @@ +using System.Collections.ObjectModel; + +namespace Falcon.DynamicSP +{ + /// + /// 存储过程返回的数据表中的行 + /// + public class DSPRow:Collection + { + } +} diff --git a/Falcon.DynamicSP/DSPTable.cs b/Falcon.DynamicSP/DSPTable.cs new file mode 100644 index 0000000..962e832 --- /dev/null +++ b/Falcon.DynamicSP/DSPTable.cs @@ -0,0 +1,11 @@ +using System.Collections.ObjectModel; + +namespace Falcon.DynamicSP +{ + /// + /// 表示存储过程返回的数据表 + /// + public class DSPTable:Collection + { + } +} diff --git a/Falcon.DynamicSP/DatabaseFacadeExtend.cs b/Falcon.DynamicSP/DatabaseFacadeExtend.cs new file mode 100644 index 0000000..2dd9287 --- /dev/null +++ b/Falcon.DynamicSP/DatabaseFacadeExtend.cs @@ -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 + { + /// + /// 执行动态存储过程。通过传入存储过程名称和参数获取返回结果 + /// + /// 数据库引用 + /// 存储过程名称 + /// 传入存储过程的参数 + /// 存储返回的数据结果 + 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; + } + + } + } + +} diff --git a/Falcon.DynamicSP/DbContextExtend.cs b/Falcon.DynamicSP/DbContextExtend.cs new file mode 100644 index 0000000..4d092c9 --- /dev/null +++ b/Falcon.DynamicSP/DbContextExtend.cs @@ -0,0 +1,19 @@ +using System.Data.SqlClient; +using Microsoft.EntityFrameworkCore; + +namespace Falcon.DynamicSP +{ + public static class DbContextExtend + { + /// + /// 对数据上下文调用存储过程,并获取返回数据 + /// + /// 数据库上下文 + /// 存储过程名称 + /// 传入存储过程的参数 + /// 存储返回的数据结果 + public static DSPTable RunDynamicSP(this DbContext db,string name,params SqlParameter[] paras) { + return db.Database.RunDynamicSP(name,paras); + } + } +} \ No newline at end of file diff --git a/Falcon.DynamicSP/Falcon.DynamicSP.csproj b/Falcon.DynamicSP/Falcon.DynamicSP.csproj new file mode 100644 index 0000000..f46d923 --- /dev/null +++ b/Falcon.DynamicSP/Falcon.DynamicSP.csproj @@ -0,0 +1,22 @@ + + + + netstandard2.0 + true + 动态调用存储过程并返回动态结果集 + false + Falcon + Falcon + + + + + + + + + + + + + diff --git a/Falcon.DynamicSP/Readme.md b/Falcon.DynamicSP/Readme.md new file mode 100644 index 0000000..19f7338 --- /dev/null +++ b/Falcon.DynamicSP/Readme.md @@ -0,0 +1,4 @@ +动态获取存储过程返回结果方法。 +调用`DatabaseFacade.RunDynamicSP`或`DbContext.RunDynamicSP`调用存储过程,传入存储过程名称和参数,可以获取一个`DSPTable`对象。 +该对象包含存储过程返回的所有行对象DSPRow,每一行对象包含一些列DSPCell对象。在列对象中存储值的相关信息。 +对于某些特殊存储过程可能无法获取返回结果架构信息,会抛出CanNotGetSchemaException异常。 \ No newline at end of file diff --git a/Falcon.ModelSP/Falcon.ModelSP.csproj b/Falcon.ModelSP/Falcon.ModelSP.csproj index 6ab81da..460b708 100644 --- a/Falcon.ModelSP/Falcon.ModelSP.csproj +++ b/Falcon.ModelSP/Falcon.ModelSP.csproj @@ -1,7 +1,8 @@  - netstandard2.0;NET461 + netstandard2.0 + true diff --git a/FalconCore.sln b/FalconCore.sln index 66a9450..4c248d4 100644 --- a/FalconCore.sln +++ b/FalconCore.sln @@ -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