【发布时间】:2019-12-31 16:31:17
【问题描述】:
我的 SQL Server 表中有几列,并且想要过滤“承包商”列。在“承包商”列中,用户可以选择两个名称“Namal”和“其他”,如屏幕截图所示。
我想要的是只为“Namal”而不是“其他”获得“金额”的总和
这是我编写但无法正常工作的代码。这里@d1 和@d2 是用户可以选择的日期范围。
Imports System.Data.SqlClient
Public Class Form6
Dim connection As New SqlConnection("server=(local); Database=Luminex; integrated security=true")
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim table As New DataTable()
Dim command As New SqlCommand("select sum(Amount) as a from PeoTVDB where Date between @d1 and @d2 and Contractor= @Namal", connection)
command.Parameters.Add("@d1", SqlDbType.Date).Value = DateTimePicker1.Value
command.Parameters.Add("@d2", SqlDbType.Date).Value = DateTimePicker2.Value
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(table)
Label1.Text = table.Rows(0)("a").ToString()
End Sub
End Class
【问题讨论】:
-
您为这个问题添加了几个不相关的标签,包括 C# 和 MySQL。请在以后的问题中使用正确的标签。
-
你也说我写的代码不行。它怎么行不通?您遇到的问题是什么。您也没有将
namal参数添加到SqlCommand。 -
很难解释我的问题,但管理员允许我发布一些截图,我可以进一步解释
-
在此处粘贴 cmets 中的链接,我会将它们放入您的问题中。澄清一下,我不是管理员或版主
-
正如其他人提到的,参数
@Namal是如何填充的?
标签: sql-server vb.net