根据不同的日期计算总和

时间:2017-08-14 22:54:46

标签: date sum

我是Stackoverflow的新手,我需要你帮助解决这个问题。 我有一个表(sql 2014和使用vb.net),如下所示:

ID  Codfisc  Dal         Al
1   xxxxxxx  2017/08/08  2017/08/14

我开发了这段代码:

Dim a As Date = Date.Parse(TextBox2.Text)
    Dim b As Date = Date.Parse(TextBox3.Text)
    Dim source2 As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Dim sql2 As String = "select SUM(DATEDIFF(day, dal, al)+1) AS Total FROM Tab1 WHERE codfisc = @codfisc and Dal >=@Dal and Al <=@al"
    Dim conn2 As New SqlConnection(source2)
    conn2.Open()
    Dim cmd2 As New SqlCommand(sql2, conn2)
    cmd2.Parameters.AddWithValue("@Codfisc", Me.GridView1.SelectedRow.Cells(8).Text)
    cmd2.Parameters.AddWithValue("@Dal", a)
    cmd2.Parameters.AddWithValue("@Al", b)
    Dim dr2 As SqlDataReader
    dr2 = cmd2.ExecuteReader()
    While dr2.Read()

        TextBox4.Text = dr2("Total").ToString()

        If IsDBNull(dr2("Total")) Then
            TextBox4.Text = "0"
        End If


    End While

    conn2.Close()

因此,如果我从2017/08/08搜索到2017/08/14textbox4.text=7并且是正确的!但是,如果我将2017/08/08搜索到2017/08/13 textbox4.text=0而不是textbox4.text=6 !!

非常感谢大家。

0 个答案:

没有答案