If CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True Then
strSQL = "SELECT name,address,phone FROM history"
ElseIf CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = False Then
strSQL = "SELECT name,address FROM history"
ElseIf CheckBox1.Checked = True And CheckBox2.Checked = False And CheckBox3.Checked = True Then
strSQL = "SELECT name,phone FROM history"
ElseIf CheckBox1.Checked = False And CheckBox2.Checked = True And CheckBox3.Checked = True Then
strSQL = "SELECT address,phone FROM history"
ElseIf CheckBox1.Checked = True And CheckBox2.Checked = False And CheckBox3.Checked = False Then
strSQL = "SELECT name FROM history"
ElseIf CheckBox1.Checked = False And CheckBox2.Checked = True And CheckBox3.Checked = False Then
strSQL = "SELECT address FROM history"
ElseIf CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = True Then
strSQL = "SELECT phone FROM history"
Else
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
แต่ว่าในงานจริงๆของผมมี CheckBox ที่จะใช้เรียกข้อมูลขึ้นมาแสดงเป็น สิบๆ CheckBox
จึงอยากจะขอ Function หรือ วิธีมาใช้ช่วยในการลดขนาดของโค้ด
รบกวนช่วยที
Dim strSQL As New StringBuilder()
strSQL.Append(" SELECT 1 ")
If CheckBox1.Checked = True Then
strSQL.Append(" ,name ")
ElseIf CheckBox2.Checked = True Then
strSQL.Append(" ,address ")
ElseIf CheckBox3.Checked = True Then
strSQL.Append(" ,phone ")
End If
strSQL.Append(" FROM table ")
strSQL.Append(" WHERE ... ")
'use strSQL.ToString()
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class FrmMuntiCheckBox
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
Dim strSQL As New StringBuilder()
strSQL.Append(" SELECT 1 ")
If CheckBox1.Checked = True Then
strSQL.Append(" ,name ")
ElseIf CheckBox2.Checked = True Then
strSQL.Append(" ,address ")
ElseIf CheckBox3.Checked = True Then
strSQL.Append(" ,phone ")
Else
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
strSQL.Append(" FROM history ")
'use strSQL.ToString()
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
da = New SqlDataAdapter(strSQL, conn) // ลองดูแล้วครับตรงนี้เกิด error
ds.Tables.Clear()
da.Fill(ds, "history")
dgvSQL.DataSource = ds.Tables("history")
TxtResult.Text = "สำเร็จ : มีข้อมูลทั้งหมด" & ds.Tables("history").Rows.Count.ToString & "แถว"
TxtRow.Text = ds.Tables("history").Rows.Count.ToString
End Sub
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class FrmMuntiCheckBox
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
Dim strSQL As New StringBuilder()
strSQL.Append(" SELECT 1 ")
If CheckBox1.Checked = True Then
strSQL.Append(" ,name ")
ElseIf CheckBox2.Checked = True Then
strSQL.Append(" ,address ")
ElseIf CheckBox3.Checked = True Then
strSQL.Append(" ,phone ")
Else
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
strSQL.Append(" FROM history ")
'use strSQL.ToString()
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
da = New SqlDataAdapter(strSQL, conn)
ds.Tables.Clear()
da.Fill(ds, "history") // error ตรงนี้ครับ
dgvSQL.DataSource = ds.Tables("history")
TxtResult.Text = "สำเร็จ : มีข้อมูลทั้งหมด" & ds.Tables("history").Rows.Count.ToString & "แถว"
TxtRow.Text = ds.Tables("history").Rows.Count.ToString
End Sub
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class FrmMuntiCheckBox
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
Dim strSQL As New StringBuilder()
strSQL.Append(" SELECT 1 ")
If CheckBox1.Checked = True Then
strSQL.Append(" ,name ")
ElseIf CheckBox2.Checked = True Then
strSQL.Append(" ,address ")
ElseIf CheckBox3.Checked = True Then
strSQL.Append(" ,phone ")
Else
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
strSQL.Append(" FROM history ")
'use strSQL.ToString()
dgvSQL.ColumnHeadersVisible = True
dgvSQL.RowHeadersVisible = True
da = New SqlDataAdapter(strSQL, conn)
ds.Tables.Clear()
da.Fill(ds, "history") // error ตรงนี้ครับ
dgvSQL.DataSource = ds.Tables("history")
TxtResult.Text = "สำเร็จ : มีข้อมูลทั้งหมด" & ds.Tables("history").Rows.Count.ToString & "แถว"
TxtRow.Text = ds.Tables("history").Rows.Count.ToString
End Sub
ช่วยทีครับ
แล้วที่บอกให้ลองใช้ common sense มันเป็นยังไง ผมยังไม่เข้าใจเลยครับ
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class FrmMuntiCheckBox
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
Dim strSQL As New StringBuilder()
strSQL.Append(" SELECT 1 ")
If CheckBox1.Checked = True Then
strSQL.Append(" ,realname ") // ขอโทษครับที่ผิดคือผมลืมไปว่าฐานข้อมูลผมใช้ "realname" ไม่ใช่ "name"
ElseIf CheckBox2.Checked = True Then
strSQL.Append(" ,address ")
ElseIf CheckBox3.Checked = True Then
strSQL.Append(" ,phone ")
Else
MessageBox.Show("กรุณาเลือกคำสั่ง", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
strSQL.Append(" FROM history ")
'use strSQL.ToString()
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 & "แถว"
TxtRow.Text = ds.Tables("history").Rows.Count.ToString
End Sub
แต่มันไม่แสดงข้อมูลต่อเรียงกันเมื่อ CheckBox1 = True And CheckBox2 = True And CheckBox3 = True พร้อมกัน
เหมือนอย่างโค้ดเก่าของผมข้างล่างนี้นะคับ
Code (VB.NET)
If CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True Then
strSQL = "SELECT realname,address,phone, FROM history"