AddWithValue不是MySql.Data.MySqlClient.MySqlParameter VB.net的成员

时间:2012-08-13 18:44:12

标签: mysql vb.net

我的代码会出现什么问题?我有一个错误AddWithValue不是MySql.Data.MySqlClient.MySqlParameter的成员

这是我的代码:

    Dim bn As String = ""
    Dim bottles As Integer = 0

    Dim SQLStatement As String = "UPDATE patient SET number_of_bottles = @bottles WHERE bednumber = @bednumber"

    Using cnn As New MySqlConnection("Server=localhost; database=patientinfo;user id=root;password=")
        Dim cmdn As New MySqlCommand(SQLStatement, cnn)
        cmdn.Parameters.AddWithValue("bottles", bottles)
        cmdn.Parameters.AddWithValue("bednumber", bn)
        cnn.Open()
        cmd.ExecuteNonQuery()

    End Using

我是VB.net和MySql的新手。任何帮助,将不胜感激。谢谢 ! :)

2 个答案:

答案 0 :(得分:1)

您必须使用您在参数中使用的名称(缺少@符号):

cmdn.Parameters.AddWithValue("@bottles", bottles)
cmdn.Parameters.AddWithValue("@bednumber", bn)

答案 1 :(得分:1)

如果您使用的是不支持“AddWithValue”的提供程序(有些则不支持),您可以使用扩展方法创建自己的提供程序。下面是一个带有一些重载的“AddWithValue”扩展方法的示例(扩展了IDbCommand)。

    ''' <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