簡體   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