Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vb.net中按MS访问的特定月份对特定列数据求和并访问文本框_Vb.net_Ms Access 2007 - Fatal编程技术网

如何在vb.net中按MS访问的特定月份对特定列数据求和并访问文本框

如何在vb.net中按MS访问的特定月份对特定列数据求和并访问文本框,vb.net,ms-access-2007,Vb.net,Ms Access 2007,无法获取特定月份的列总和值 我试图从MS Access获取文本框或标签的数据,但结果为空 Dim Month As String = dtInput.Value.ToString("MMM") Dim rs As ADODB.Recordset Dim str As String = "" str = "Select Sum(Input_Amount) FROM Input_Paid Where Month(Dt_Entry)='" & Month &am

无法获取特定月份的列总和值

我试图从MS Access获取文本框或标签的数据,但结果为空

    Dim Month As String = dtInput.Value.ToString("MMM")
    Dim rs As ADODB.Recordset
    Dim str As String = ""
    str = "Select Sum(Input_Amount) FROM Input_Paid Where Month(Dt_Entry)='" & Month & "'"

    rs = New ADODB.Recordset
    dbconnect1()

    rs.Open(str, conne, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockPessimistic)

    lblInputtotalInputDeposite.Text = "Total Deposit Input this Month - " & rs.Fields(0).Value
    rs.Close()
    rs = Nothing
I除了输出不为空外,这里还有一些结果:

(rs.Fields(0).Value)

切勿将文字用于月份筛选,因为这些文字是本地化的:

Dim Month As Integer=dtInput.Value.Month()
Dim Year As Integer=dtInput.Value.Year()
将rs设置为ADODB.Recordset
Dim str As String=“”
str=“从输入中选择金额(输入金额),其中年份(输入金额项)=“&Year&”和月份(输入金额项)=“&Month&”
dbconnect1()
rs=新的ADODB.Recordset
rs.Open(str,conne,ADODB.CursorTypeEnum.adOpenStatic,DODB.LockTypeEnum.adlock悲观)
lblinputtalinputdeposite.Text=“本月存款总额输入-”&rs.Fields(0).Value
rs.Close()
rs=无

现在,让我们用ADO.net的方式来做

将数据库对象保持在本地,以便控制打开、关闭和处理。后两个是通过使用…结束使用块来完成的

要获取有关连接字符串的帮助,请参阅

最好使用参数来避免sql注入。Access不关心参数名称。如果只关心它们在命令文本中出现的顺序是否与它们添加到参数集合的顺序相匹配

ExecuteScalar返回结果集中第一行的第一列。在本例中,总和为。它返回一个对象,因此需要转换为十进制

只有在连接关闭后,我们才更新用户界面。我使用了插值字符串

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Total As Decimal
    Using cn As New OleDbConnection("Your connection string")
        Using cmd As New OleDbCommand("Select Sum(Input_Amount) FROM Input_Paid Where Month(Dt_Entry)= @Month And Year(Dt_Entry) = @Year;", cn)
            cmd.Parameters.Add("@Month", OleDbType.Integer).Value = dtInput.Value.Month
            cmd.Parameters.Add("@Year", OleDbType.Integer).Value = dtInput.Value.Year
            cn.Open()
            Total = CDec(cmd.ExecuteScalar)
        End Using
    End Using 'closes and disposes the connection
    lblInputtotalInputDeposite.Text = $"Total Deposit Input this Month is {Total}"
End Sub

不同的是,您将缩写月份名称与月份数进行比较