这些扩展方法看起来正确吗?

发布于 2024-10-12 19:55:11 字数 1621 浏览 3 评论 0原文

在 VB.NET 中,我正在为 SqlParameterCollection 类创建扩展方法,以便我可以在一行代码中添加“详细”参数。据我所知,每次通过带有未指定字母数字数据类型(即 VarChar 或 NVarChar)大小的 SqlParameter 对象的 SqlCommand 调用存储过程时,.AddWithValue 方法都会导致 SQL Server 重新评估 SQL Server 执行计划。换句话说,.AddWithValue 是一个坏主意,除了像 INT 数据类型这样的类型,其中数据类型的大小是恒定的。

我以前从未使用过扩展方法,那么这段代码似乎可以实现我想要的功能吗?代码“有效”,但我正在寻找另一双眼睛,以确保我不会得到将强制重新评估 SQL Server 执行计划的参数。

编辑:我必须将参数实际添加到“params”集合中,因此我更新了代码。现在它“有效”了;-)

Imports System.Runtime.CompilerServices

Public Module AdoParameterExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddWithNumericValue(ByVal params As System.Data.SqlClient.SqlParameterCollection, ByVal parameterName As String, ByVal parameterType As SqlDbType, ByVal value As Object)

        Dim newParam As New System.Data.SqlClient.SqlParameter

        With newParam

            .ParameterName = parameterName
            .SqlDbType = parameterType
            .Value = value

        End With

        params.Add(newParam)

    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddWithStringValue(ByVal params As System.Data.SqlClient.SqlParameterCollection, ByVal parameterName As String, ByVal parameterType As SqlDbType, ByVal size As Integer, ByVal value As Object)

        Dim newParam As New System.Data.SqlClient.SqlParameter

        With newParam

            .ParameterName = parameterName
            .SqlDbType = parameterType
            .Size = size
            .Value = value

        End With

        params.Add(newParam)

    End Sub

End Module

感谢您的反馈。

In VB.NET, I am creating extension methods for the SqlParameterCollection class so that I can add "detailed" parameters in one line of code. I understand that the .AddWithValue method will cause SQL Server to re-evaluate an SQL Server execution plan each time a Stored Procedure is called via an SqlCommand with SqlParameter objects that do not specify the size of alphanumeric data types (i.e. VarChar or NVarChar). In other words, .AddWithValue is a bad idea except for types like INT data types where the size of the data type is constant.

I've never used Extension Methods before, so does this code appear to accomplish what I want? The code "works", but I'm looking for another set of eyes to make sure I'm not ending up with parameters that will force re-evaluation of an SQL Server execution plan.

EDIT: I had to actually add the parameter to the "params" collection, so I've updated the code. Now it "works" ;-)

Imports System.Runtime.CompilerServices

Public Module AdoParameterExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddWithNumericValue(ByVal params As System.Data.SqlClient.SqlParameterCollection, ByVal parameterName As String, ByVal parameterType As SqlDbType, ByVal value As Object)

        Dim newParam As New System.Data.SqlClient.SqlParameter

        With newParam

            .ParameterName = parameterName
            .SqlDbType = parameterType
            .Value = value

        End With

        params.Add(newParam)

    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddWithStringValue(ByVal params As System.Data.SqlClient.SqlParameterCollection, ByVal parameterName As String, ByVal parameterType As SqlDbType, ByVal size As Integer, ByVal value As Object)

        Dim newParam As New System.Data.SqlClient.SqlParameter

        With newParam

            .ParameterName = parameterName
            .SqlDbType = parameterType
            .Size = size
            .Value = value

        End With

        params.Add(newParam)

    End Sub

End Module

Thanks for your feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葮薆情 2024-10-19 19:55:11

你的扩展看起来不错。使用扩展方法不会影响 SQL Server 执行计划是否重新评估。

我提出的一项建议是返回 SqlParameterCollection 而不是 void (Sub)。然后可以流畅地添加参数。

而不是

params.AddWithNumericValue(...)
params.AddWithNumericValue(...)
params.AddWithNumericValue(...)

你写为

params.AddWithNumericValue(...).AddWithNumericValue(...).AddWithNumericValue(...)

这是一种偏好,但有了这样的扩展,我开始欣赏这种风格。

Your extensions look fine. Whether or not the SQL Server execution plan well be re-evaluated won't be affected by using an Extension method.

One recommendation I'd make is to return the SqlParameterCollection instead of void (Sub). Parameters can then be added fluently.

Instead of

params.AddWithNumericValue(...)
params.AddWithNumericValue(...)
params.AddWithNumericValue(...)

you write as

params.AddWithNumericValue(...).AddWithNumericValue(...).AddWithNumericValue(...)

It's a preference, but with extensions like this I've come to appreciate the style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文