2014年11月11日 星期二

VB SQL 資料庫讀取資料

一、抓到 datagridview
Dim sqlstr As New String("SELECT   年度薪資.姓名, 年度薪資.員工編號" & _
        " FROM    where year(年度薪資.發薪日期)=" & TextBox1.Text & " group by 年度薪資.姓名, 年度薪資.員工編號 ;")

        Dim da As New SqlDataAdapter(sqlstr, MDIParent1.conn)
        Dim cb As New SqlCommandBuilder(da)

        da.Fill(Data)
        DataGridView1.DataSource = Data

二、reader 一筆一筆讀取
Dim sqlquery As String
      sqlquery = "select count(姓名) from 考核獎金 where 年度='" & TextBox1.Text & "' and 次數='" & TextBox2.Text & "';"
        Dim cmnd As SqlCommand = New SqlCommand(sqlquery, MDIParent1.conn)  
        Dim reader As SqlDataReader
        reader = cmnd.ExecuteReader()
        While (reader.Read())
            If reader.IsDBNull(0) = False Then
                havedata = reader.GetValue(0)
            End If
        End While
        reader.Close()

2014年5月15日 星期四

VB 計算每月的例假日天數

 Public Function GetholiDays(ByVal theYear As Integer, ByVal theMonth As Integer) As Integer
        Dim theDays As Integer = 8
        Dim x, y As Integer
        Dim dt As New DateTime(theYear, theMonth, 1)
        x = DateTime.DaysInMonth(theYear, theMonth) - 28

        If dt.DayOfWeek.ToString() = "Monday" Then y = 1
        If dt.DayOfWeek.ToString() = "Tuesday" Then y = 2
        If dt.DayOfWeek.ToString() = "Wendnesday" Then y = 3
        If dt.DayOfWeek.ToString() = "Thursday" Then y = 4
        If dt.DayOfWeek.ToString() = "Friday" Then y = 5
        If dt.DayOfWeek.ToString() = "Saturday" Then y = 6
        If dt.DayOfWeek.ToString() = "Sunday" Then y = 7
     
        If x + y = 7 Then
            theDays = 9
        ElseIf x + y >= 8 Then
            theDays = 10
        End If
        If y = 7 Then theDays = theDays - 1

        Return theDays
    End Function