  | 
              
	              
	                
  
    |   | 
   
  
    
        
        สอบถามเกี่ยวกับการสร้างฟอร์มและวิธีการใช้คำสั่ง  SQL เพื่อให้ User เลือกข้อมูลที่ต้องการได้     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Dim sSQL As String = Trim(Me.Textbox1.Text)
objCom = New SqlCommand(sSQL, dbCon)
objCom.ExecuteNonQuery()
  
 
ผมเข้าใจอะไรผิดหรือเปล่า                          
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-11 20:36:57 | 
                        By :
                            Downrung | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เข้าใจถูกแล้วครับคุณต้น 
 
Code  ด้านล่างนี้เป็น Code  ที่สามารถให้ User  เลือกข้อมูลที่ไม่ต้องการได้แต่ ได้แค่  MachineID ตัวเดียว แล้วถ้าหากเราต้องการให้ User เลือกข้อมูลที่ไม่ต้องการมากกว่า 1  ตัว  ต้องทำอย่างไรครับ แต่ถ้าเกิดเราเพิ่ม Textbox  เท่ากับจำนวนที่ User  ไม่ต้องการก็จะสามารถทำได้แต่ในกรณีนี้เราไม่ทราบว่าข้อมูลที่ User  ไม่ต้องการมีข้อมูลมากน้อยเท่าไร 
 
รบกวนด้วยครับ  ขอบคุณครับ 
 
Code (VB.NET) 
Sub InitialMachine()
        Dim cmd As New SqlCommand
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Try
            With cmd
                .Connection = mycon
                .CommandType = CommandType.Text
                .CommandText = "select MachineName from T_Machine where MachineID<>'" & TextBox1.Text & "' "
                ' .CommandText = " '" & TextBox1.Text & "' "
            End With
            da.SelectCommand = cmd
            da.Fill(ds, "Matco1_Machine") 'Keep data to Dataset
            cboMachineName.DataSource = ds.Tables("Matco1_Machine")
            cboMachineName.DisplayMember = "MachineName"
            cboMachineName.ValueMember = "MachineName"
        Catch exsql As SqlException
            MsgBox(exsql.Message, MsgBoxStyle.Exclamation)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-12 08:12:05 | 
                        By :
                            s_oil_p | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แนะนำแนวคิดนะครับเป็นการใช้ textbox รับค่า MachineID จาก Textbox และถ้าจะรับค่าที่ไม่ต้องการเพิ่ม ให้ใช้ Button กดปุ่ม และเขียนคำสั่งประมาณว่า AND MachineID <> textbox1.text ต่อท้าย Command text เข้าไปครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-12 08:41:38 | 
                        By :
                            01000010 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 งง  ครับ  คุณ 0100 0010 พอดีผมพึ่งหัดเขียนครับ   
พอมี  Code ตัวอย่าง  อธิบายได้หรือเปล่าครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-12 08:53:03 | 
                        By :
                            s_oil_p | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เอางี้มั้ยครับ ใช้ listview  with checkbox มาช่วย 
โดยเราก็ดึงเอาชื่อคอลัมน์มาใส่ใน listview ให้ user เลือก 
 
** กรณีนี้มีปัญหา 2 ข้อ คือ 
1. คิวรี่ได้ทีละตาราง  หากมากกว่า 1 ตาราง ต้องมีการ join กัน  สามารถทำได้แต่ซับซ้อนหน่อยนะครับ 
2. user จะรู้ชื่อคอลัมน์หรือเปล่า  
 
เปิดโปรแกรมมาเราดึงชื่อตารางมาใส่ combobox ไว้รอ 
  
 
 
หลังจากเลือกชื่อตารางใน combobox ให้ดึงชื่อคอลัมน์ของตารางนั้นๆมาใส่ใน listview  
หลังจากนั้นเมื่อกดปุ่ม >> เราก็จัดการสร้างคิวรี่ดึงข้อมูลและแสดงผล 
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-12 10:15:47 | 
                        By :
                            คนงานตัดอ้อย | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณครับ คุณคนงานตัดอ้อย 
 
หลังจากนำมาไว้ใน listView  แล้วเราก็ วนลูป หาค่าเพื่อไปใส่ใน  .commantext  ใช่หรือเปล่าครับ  พอมีตัวอย่าง  Code  หรือเปล่าครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-12 10:43:09 | 
                        By :
                            s_oil_p | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ดึงชื่อตารางทั้งหมดมาใส่ใน combobox 
Code (VB.NET) 
Dim sql1 As String = "SELECT * FROM SYS.TABLES ORDER BY Name"
Dim dt As New DataTable
dt = gGetDataTable(sql1)
If dt IsNot Nothing Then
            If dt.Rows.Count > 0 Then
                For Each dr As DataRow In dt.Rows
                    Me.cboTableName.Items.Add(dr("Name"))
                Next
            End If
            dt.Dispose()
            dt = Nothing
End If
 
 
ดึงชื่อคอลัมน์ของตารางที่เลือกจาก combobox มาใส่ใน listview 
Code (VB.NET) 
Private Sub cboTableName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTableName.SelectedIndexChanged
        Me.lvwColumn.Items.Clear()
        Dim sql1 As String = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = " & gSQ(Me.cboTableName.Text)
        Dim dt As New DataTable
        dt = gGetDataTable(sql1)
        If dt IsNot Nothing Then
            If dt.Rows.Count > 0 Then
                Dim lvwItem As ListViewItem
                For Each dr As DataRow In dt.Rows
                    lvwItem = lvwColumn.Items.Add(dr("COLUMN_NAME"))
                Next
            End If
            dt.Dispose()
            dt = Nothing
        End If
    End Sub
 
 
 
จัดการคิวรี่และแสดงข้อมูล 
Code (VB.NET) 
Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Dim tempCol As String = " "
        For i As Integer = 0 To Me.lvwColumn.Items.Count - 1
            If Me.lvwColumn.Items(i).Checked Then
                tempCol &= Me.lvwColumn.Items(i).SubItems(0).Text & ", "
            End If
        Next
        If (tempCol.Equals(" ")) Then
            Exit Sub
        End If
        Dim newStr As String = tempCol.Remove(tempCol.Length - 2, 2)
        newStr &= " "
        Dim sql1 As String = "SELECT " & newStr & " FROM " & Me.cboTableName.Text
        Dim dt As New DataTable
        dt = gGetDataTable(sql1)
        If dt IsNot Nothing Then
            Me.dgv1.DataSource = dt
            dt.Dispose()
            dt = Nothing
        End If
End Sub
 
 
ฟังก์ชั่นสำหรับคิวรี่ข้อมูล 
Code (VB.NET) 
Public Function gGetDataTable(ByVal pQryString As String, _
                                  Optional ByVal pConn As SqlClient.SqlConnection = Nothing, _
                                  Optional ByVal pIsSilenceMode As Boolean = False) As DataTable
        If pQryString Is Nothing OrElse pQryString.Length = 0 Then
            Return Nothing
        End If
        Dim dt As New DataTable
        Dim da As New SqlClient.SqlDataAdapter
        Dim conn As New SqlClient.SqlConnection("ตรงนี้ใส่ Connection String ของคุณลงไป")
        If pConn IsNot Nothing Then
            conn = pConn
        End If
        Try
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
        Catch ex As Exception
            Return Nothing
        End Try
        Try
            Dim qry As String = pQryString
            If pConn IsNot Nothing Then
                da = New SqlClient.SqlDataAdapter(qry, conn)
            Else
                da = New SqlClient.SqlDataAdapter(qry, conn)
            End If
            da.SelectCommand.CommandTimeout = 0
            da.Fill(dt)
        Catch ex As Exception
            If Not pIsSilenceMode Then
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Finally
            If da IsNot Nothing Then
                da.Dispose()
                GC.SuppressFinalize(da)
                da = Nothing
            End If
        End Try
        Return dt
End Function
 
 
 
ฟังก์ชั่นเสริมเล็กๆ 
Code (VB.NET) 
Private Function gSQ(ByVal pText As String) As String
        Return "'" & pText.Replace("'", "") & "'"
End Function
 
 
 
 
 
 
 
  หลายๆครั้งในชีวิต  กำลังใจก็สำคัญเหนือสิ่งอื่นใด                          
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2013-06-12 13:22:24              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-06-12 11:02:05 | 
                        By :
                            คนงานตัดอ้อย | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |