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()