简体   繁体   English

使用Dapper从T-SQL存储过程返回XML字符串

[英]Using Dapper to return XML string from T-SQL stored procedures

I am working on a project to convert a large VB6 application to .NET. 我正在做一个将大型VB6应用程序转换为.NET的项目。 I decided to create a project to provide a facade to the existing VB6 ADO code, where I am using the amazing Dapper extension methods to handle all the database code that the VB6 ADO functions used to do. 我决定创建一个项目,为现有的VB6 ADO代码提供外观,在这里我使用令人惊叹的Dapper扩展方法来处理VB6 ADO函数用来完成的所有数据库代码。

One of the features I have to support in my new project is the ability to get the XML string results from T-SQL stored procedures (via the FOR XML). 我在新项目中必须支持的功能之一是能够从T-SQL存储过程(通过FOR XML)获取XML字符串结果的能力。 Dapper doesn't have support to return this XML that I can see. Dapper不支持返回我可以看到的XML。 So, I implemented the ADO.NET ExecuteXmlReader method to provide this return. 因此,我实现了ADO.NET ExecuteXmlReader方法来提供此返回。 My project is also using Dapper DynamicParameters to capture all the in/out parameters required for the stored procedures. 我的项目还使用Dapper DynamicParameters捕获存储过程所需的所有输入/输出参数。

What I don't see how to do, is how to convert the DynamicParameters to the SqlCommand.SqlParameterCollection so that I can populate these parameters into the SqlCommand object for the ExecuteXmlReader method. 我看不到怎么做,是如何将DynamicParameters转换为SqlCommand.SqlParameterCollection以便可以将这些参数填充到ExecuteXmlReader方法的SqlCommand对象中。 I have to support output parameters also for this scenario. 对于这种情况,我也必须支持输出参数。

I can iterate over the DynamicParameters , but that only gets me the parameter name, and value. 我可以遍历DynamicParameters ,但这只会为我获取参数名称和值。 I also need the direction, type, size, scale, and precision. 我还需要方向,类型,大小,比例和精度。 Dapper has a DynamicParameters.ReplaceLiterals method that takes an IDbCommand object for replacing literals in SQL string. Dapper具有DynamicParameters.ReplaceLiterals方法,该方法采用IDbCommand对象来替换SQL字符串中的文字。 I wish it had a method to also fill in the parameters. 我希望它有一种也可以填写参数的方法。

Am I missing something obvious here? 我在这里错过明显的东西吗? If I call the Dapper Execute method, I can pass the DyanmicParameters directly into this method. 如果我调用Dapper Execute方法,则可以将DyanmicParameters直接传递到此方法中。 I'm just not seeing how to pass them to the SqlCommand object. 我只是没有看到如何将它们传递给SqlCommand对象。

After testing, I see that Dapper does indeed support pulling the XML from stored procedures. 经过测试,我发现Dapper确实支持从存储过程中提取XML。

var result = conn.Query<string>(@"select * from <someTable> for xml auto");

This will return an array of string with each element containing up to 2,033 characters, which you can simple join to have your result as a single string. 这将返回一个字符串数组,其中每个元素最多包含2,033个字符,您可以简单地将其连接起来以将结果作为单个字符串。

var fullResult = string.Join("", result);

or 要么

var fullResult = string.Concat(result);

or, all in one step: 或全部一步:

var result = string.Concat(conn.Query<string>(
    @"select * from <someTable> for xml auto", buffered: false));

So, there is no need for me to implement ExcuteXmlReader method myself, and now I can let Dapper handle the parameters normally. 因此,我不需要自己实现ExcuteXmlReader方法,现在我可以让Dapper正常处理参数。

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

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