簡體   English   中英

如何在mysql和visual basic中設置變量組?

[英]How to set variable group in mysql and visual basic?

如何正確設置@dategiven1?

set @dategiven1 = ('1','2','3');
set @dategive4 = 2020;
set @activity = "Aerial Cable Installation";

SELECT id, billingreference, opac, projectorder, projectlocation, headedby, dategiven 
FROM project_info 
WHERE MONTH(dategiven) IN (@dategiven1) 
  AND YEAR(dategiven) = @dategiven4 
  AND id IN ( SELECT project_id 
              FROM manpower_project 
              WHERE activity = @activity )

我怎樣才能應用這個視覺基礎?

謝謝,關注我無法解釋的問題:(

似乎 MySql 中的 Month 方法返回一個整數,因此您需要用括號括起來的逗號分隔的數字。 一個參數傳遞一個單一的值,所以這里的參數是不合適的。 您需要構建一個字符串並將其連接到您的 select 語句中。

Private Function InClause(Months As Integer(), Year As Integer, Activity As String) As DataTable
    'StringBuilder is mutable so it save creating a new string and tossing the old one on each iteration
    'Start the string out with the opening parenthesis
    Dim BuildInClause As New StringBuilder("(")
    'loop through each element in the array and add it to the string builder along with a comma
    For Each m In Months
        BuildInClause.Append(m & ",")
    Next
    'convert the StringBuilder to a string, Trim off the final comma and add the closing parenthesis.
    Dim str = BuildInClause.ToString.Trim(","c) & ")"
    'Check what the string looks like
    Debug.Print(str)
    Dim dt As New DataTable
    'The Using...End Using block ensures that you connection and command are closed and disposed even if there is an error
    Using cn As New MySqlConnection("Your connection String"),
            cmd As New MySqlCommand("Select id, billingreference, opac, projectorder, projectlocation, headedby, dategiven 
                From project_info
                Where Month(dategiven) In " & str &
                " And YEAR(dategiven) = @dategiven4 
                And id IN ( SELECT project_id From manpower_project 
                  Where activity = @activity ))", cn)
        cmd.Parameters.Add("@dategiven4", MySqlDbType.Int32).Value = Year
        cmd.Parameters.Add("@activity", MySqlDbType.String).Value = Activity
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

用法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim intArray = {1, 2, 3}
    Dim dt = InClause(intArray, 2020, "Aerial Cable Installation")
    DataGridView1.DataSource = dt
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM