繁体   English   中英

如何为mysql变量添加参数?

[英]How can I add a parameter for mysql variable?

我在 mysql 中有一个查询,其中包含一个变量。 查询是

SELECT
  GROUP_CONCAT(
    CONCAT(
      ' MAX(IF(Property = ''',
      t.Property,
      ''', Value, NULL)) AS ',
      t.Property
    )
  ) INTO @PivotQuery
FROM
  (SELECT
     Property
   FROM
     ProductOld
   GROUP BY
     Property) t;

SET @PivotQuery = CONCAT('SELECT ProductID,', @PivotQuery, ' FROM ProductOld GROUP BY ProductID'); 

PREPARE statement FROM @PivotQuery;
EXECUTE statement;
DEALLOCATE PREPARE statement;

但是,当我在 vb.net 应用程序中使用 mysqlcommand 运行时,它会抛出一个错误,指出必须定义参数 @PivotQuery..

将此代码与您现有的代码交换:SET @PivotQuery = CONCAT('SELECT ProductID',@PivotQuery,'FROM ProductOld GROUP BY ProductID');

Private Function saveCustomer()
Dim command As MySqlCommand = Nothing
Dim query As String = "INSERT INTO contacts (first_name, surname, house_number, street, suburb, state, phone, mobile, work, email, notes) VALUES (@first_name, @surname, @housenumber, @street, @suburb, @state, @phone, @mobile, @work, @email, @notes)"
Try
    If connection.State = ConnectionState.Closed Then
        connection.Open()

    End If
    command = New MySqlCommand(query, connection)
    command.Parameters.AddWithValue("@first_name", txtFirstName.Text)
    command.Parameters.AddWithValue("@surname", txtSurname.Text)
    command.Parameters.AddWithValue("@housenumber", txtHouseNo.Text)
    command.Parameters.AddWithValue("@street", txtStreet.Text)
    command.Parameters.AddWithValue("@suburb", txtSuburb.Text)
    command.Parameters.AddWithValue("@state", cboState.Text)
    command.Parameters.AddWithValue("@phone", txtPhone.Text)
    command.Parameters.AddWithValue("@mobile", txtMobile.Text)
    command.Parameters.AddWithValue("@work", txtWork.Text)
    command.Parameters.AddWithValue("@email", txtEmail.Text)
    command.Parameters.AddWithValue("@notes", txtNotes.Text)
    command.ExecuteNonQuery()
    MessageBox.Show("Contact Saved Sucessfully")
    Return True
Catch ex As MySqlException
    Return False
Finally
    connection.Close()
    command.Dispose()
End Try

暂无
暂无

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

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