Imports System.Data
Imports System.Data.SqlClient
Public Class FrmCheckBox_SQL
Dim conn As New SqlConnection
Dim da As SqlDataAdapter
Dim ds As New DataSet()
Private Sub BtnExecSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExecSQL.Click
Dim strconn As String = ""
strconn = "Data Source=SGH120QXPK-HP;Initial Catalog=dbTEST2;Integrated Security=True"
If conn.State = ConnectionState.Open Then
conn.Close()
Else
conn.ConnectionString = strconn
conn.Close()
End If
Try
Dim strSQL As String = ""
If CheckBox1.Checked = True Then
strSQL = "SELECT name,subname FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
ElseIf CheckBox2.Checked = True Then
strSQL = "SELECT address,phone FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
ElseIf CheckBox3.Checked = True Then
strSQL = "SELECT Office FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
End If
da = New SqlDataAdapter(strSQL, conn)
ds.Tables.Clear()
da.Fill(ds, "history")
dgvSQL.DataSource = ds.Tables("history")
TxtResult.Text = "สำเร็จ : มีข้อมูลทั้งหมด" & ds.Tables("history").Rows.Count.ToString & "แถว"
Catch ex As Exception
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Dim strSQL As String = String.Empty
If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked Then
strSQL = "SELECT name,subname,address,phone,Office FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
End If
If CheckBox1.Checked = True Then
strSQL = "SELECT name,subname FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
ElseIf CheckBox2.Checked = True Then
strSQL = "SELECT address,phone FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
ElseIf CheckBox3.Checked = True Then
strSQL = "SELECT Office FROM history"
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
End If
Private Sub BtnExecSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExecSQL.Click
Dim strconn As String = ""
strconn = "Data Source=SGH120QXPK-HP;Initial Catalog=dbTEST2;Integrated Security=True"
If conn.State = ConnectionState.Open Then
conn.Close()
Else
conn.ConnectionString = strconn
conn.Close()
End If
Try
Dim strSQL As String = ""
If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked Then
CheckBox123.Checked = True
End If
If CheckBox123.Checked = True Then
strSQL = "SELECT realname,subname,address,phone,Office FROM history"
ElseIf CheckBox1.Checked = True Then
strSQL = "SELECT realname,subname FROM history"
ElseIf CheckBox2.Checked = True Then
strSQL = "SELECT address,phone FROM history"
ElseIf CheckBox3.Checked = True Then
strSQL = "SELECT Office FROM history"
End If
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
da = New SqlDataAdapter(strSQL, conn)
ds.Tables.Clear()
da.Fill(ds, "history")
dgvSQL.DataSource = ds.Tables("history")
TxtResult.Text = "สำเร็จ : มีข้อมูลทั้งหมด" & ds.Tables("history").Rows.Count.ToString & "แถว"
Catch ex As Exception
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub