0

我的代码会有什么问题?我有一个错误 AddWithValue is not member of 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 的新手。任何帮助,将不胜感激。谢谢 !:)

4

2 回答 2

1

如果您使用的提供程序不支持“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
于 2012-08-13T19:52:51.033 回答
1

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

cmdn.Parameters.AddWithValue("@bottles", bottles)
cmdn.Parameters.AddWithValue("@bednumber", bn)
于 2012-08-13T18:52:56.803 回答