如果您使用的提供程序不支持“AddWithValue”(有些不支持),您可以使用扩展方法制作自己的。这是一个带有一些重载(扩展 IDbCommand)的“AddWithValue”扩展方法的示例。
''' <summary>
''' Adds a parameter into the IDbCommand and sets it's name, value and type.
''' </summary>
''' <param name="cmd"></param>
''' <param name="paramName"></param>
''' <param name="paramValue"></param>
''' <param name="dbType"></param>
''' <remarks></remarks>
<Extension()> _
Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object, ByVal dbType As DbType)
Dim param As DbParameter = cmd.CreateParameter
param.ParameterName = paramName
param.DbType = dbType
param.Value = paramValue
cmd.Parameters.Add(param)
End Sub
''' <summary>
''' Adds a parameter into the IDbCommand and name and value.
''' </summary>
''' <param name="cmd"></param>
''' <param name="paramName"></param>
''' <param name="paramValue"></param>
''' <remarks></remarks>
<Extension()> _
Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object)
Dim param As DbParameter = cmd.CreateParameter
param.ParameterName = paramName
param.Value = paramValue
cmd.Parameters.Add(param)
End Sub
''' <summary>
''' Adds a parameter into the IDbCommand and sets it's name, value and type.
''' </summary>
''' <param name="cmd"></param>
''' <param name="paramName"></param>
''' <param name="paramValue"></param>
''' <param name="dbType"></param>
''' <remarks></remarks>
<Extension()> _
Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object, ByVal dbType As DbType, ByVal size As Integer)
Dim param As DbParameter = cmd.CreateParameter
param.ParameterName = paramName
param.DbType = dbType
param.Value = paramValue
param.Size = size
cmd.Parameters.Add(param)
End Sub