简体   繁体   English

确定SQL Server查询(存储过程)结果类型

[英]Determine SQL Server query (stored procedure) result type

I am planning to organize my data in SQL Server as a small orm of my own, creating classes of meta data on each in my code. 我计划将我的数据组织在SQL Server中作为我自己的一小部分,在我的代码中为每个数据创建元数据类。

For the tests I am hard-coding the objects, the next step is to generate the properties of each using SQL Server queries about those objects. 对于测试我是对对象进行硬编码,下一步是使用SQL Server查询生成每个对象的属性。

And now that I deal with the stored procedures section of my code in C#, 现在我在C#中处理我的代码的存储过程部分,
I was wondering how it is possible to somehow use SQL Server to query the result type of the command executed? 我想知道如何以某种方式使用SQL Server查询执行的命令的结果类型?

For example, here we know what it's doing even by reading its name ... 例如,在这里我们甚至通过阅读它的名字就知道它在做什么......

[dbo].[GtallDrLFilesCount]

but another could select some other type of return such as rowset string etc' 但另一个可以选择其他类型的返回,如行集字符串等'

Using the above stored procedure will return an int : 使用上面的存储过程将返回一个int:

if(@AllDrives=1) Begin 
        Select 
          * From [dbo].[HddFolderFiles]
End

but the next (above) selects all content rather the RowsCount 但是下一个(上面)选择所有内容而不是RowsCount

I was planning to access SQL Server and query it's objects, and as I do not plan to set return parameter (OUT), is there a more elegant way to achieve it, rather than parsing the .sql file of the stored procedure? 我打算访问SQL Server并查询它的对象,因为我不打算设置返回参数(OUT),是否有更优雅的方法来实现它,而不是解析存储过程的.sql文件?

Like if text contains SELECT * (this is a rowset) expect it with DataTable if text contains Select COUNT(*) (this is int) prepare int type variable. 就像文本包含SELECT * (这是一个行集)一样,如果text包含Select COUNT(*) (这是int),则使用DataTable准备int类型变量。

I thought in the case I did not assign an out parameter to my stored procedures can SQL Server tell the return type somehow even though it has no out parameter to make it easy for it? 我认为在我没有为我的存储过程分配一个out参数的情况下,SQL Server能以某种方式告诉返回类型,即使它没有out参数来使它变得容易吗?

I think you would have to execute the SProc to get it's columns, but you could do it without actually returing data using set fmtonly 我认为你必须执行SProc来获取它的列,但你可以在不使用set fmtonly实际撤回数据的情况下执行它

Even sprocs that return a single value (eg - int) return a table when you use c# ... so you just need to take a look at the reader's Columns to get the data you want. 甚至返回单个值的sprocs(例如 - int)在您使用c#时返回一个表...所以您只需要查看读者的列以获取所需的数据。

So: 所以:

set fmtonly on
exec [dbo].[MyStoredProc] 0
set fmtonly off

Will return a recordset which you can examine in c# 将返回一个可以在c#中检查的记录集

var adoCon = new System.Data.SqlClient.SqlConnection(_sConnectStr);
var adoCmd = new System.Data.SqlClient.SqlCommand("your SQL (above)", adoCon);

var Rows = adoCmd.ExecuteReader();
DataTable dtSchema = Rows.GetSchemaTable();

Now - you can wander through dtSchema to get columns. 现在 - 您可以浏览dtSchema以获取列。 It's not pure SQL, but it's ac# + SQL approach. 它不是纯SQL,而是ac#+ SQL方法。 [dbo].[GtallDrLFilesCount] will return a single column table (column of type int). [dbo].[GtallDrLFilesCount]将返回单个列表(int类型的列)。

Obviously - use a SQL command (not string). 显然 - 使用SQL命令(不是字符串)。 The next trick is translating SQL types into native c# types (easy for some data types and tricky for others ... take a look at ADOCommand's ReturnProviderSpecificTypes option). 下一个技巧是将SQL类型转换为本机c#类型(对于某些数据类型很容易,对其他数据类型很棘手......看看ADOCommand的ReturnProviderSpecificTypes选项)。

Hope that helps! 希望有所帮助!

From SQL Server 2012+ you can use sys.dm_exec_describe_first_result_set to read metadata about resultset: SQL Server 2012+您可以使用sys.dm_exec_describe_first_result_set来读取有关结果集的元数据:

This dynamic management function takes a Transact-SQL statement as a parameter and describes the metadata of the first result set for the statement. 此动态管理功能将Transact-SQL语句作为参数,并描述该语句的第一个结果集的元数据。

SELECT *
FROM sys.dm_exec_describe_first_result_set(
   N'EXEC [dbo].[MyProcedure]', NULL, 0);

SELECT *
FROM sys.dm_exec_describe_first_result_set(
   N'SELECT * FROM [dbo].[tab]', NULL, 0);

SqlFiddleDemo

This method has limitation for more info read Remarks section 此方法有更多信息阅读备注部分的限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM