mysql - 如何在mysql和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 方法似乎返回一个 Integer,因此,您需要用括号括起来的逗号分隔数字。参数传递单个值,因此此处不适合使用参数。您需要构建一个字符串并将其连接到您的 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
推荐阅读
- applepay - 使用支付请求 API 处理 Apple Pay 中的错误
- spring-boot - 如何有选择地为特定的执行器 url 添加端口?
- javascript - 如何按三个属性对数据进行排序?
- java - Aws 步进函数 - 从失败的步进函数活动中恢复,而不是开始新的执行
- python - 在生成器修改变量后获取变量的初始状态
- ios - Flutter IOS黑屏
- c# - 用于转义一些单引号的正则表达式
- django - 如何在 Django Forms 中手动呈现清除复选框?
- sql - 表获取新记录时自动在另一条记录中创建记录
- .net - 如何在 Tally 中获取创建的公司列表?