如何使用多个文本框查询我的数据

时间:2014-03-01 03:19:40

标签: sql vb.net

我有名为classno 1到8的文本框

我有这样的代码:

Dim sqlQuery As String = "SELECT ClassNo, SubjectCode, Title, Lec, Lab, Days, 
TimeSlot, roomNo, Ins_ID 
from studSched WHERE SubjectCode = '" & classno1.Text & "' and '" & classno2.Text & "' and '" & classno3.Text & 
"' AND'" & classno4.Text & "' AND'" & classno5.Text & "' AND '" & classno6.Text & "' AND'" & classno7.Text & "' AND'" & classno8.Text & "' "

其中textbox classno 1到8的值为subject 1到8

例如: classno1 = “subject1”

如何查询我访问中的所有文本框

我的探测器是它将获取我访问中的所有数据, 如何在我的访问中获取所有特定数据..

我需要一个查询代码..

1 个答案:

答案 0 :(得分:0)

我认为你需要这个

Dim sqlQuery As String = "SELECT ClassNo, SubjectCode, Title, Lec, 
                                 Lab, Days,TimeSlot, roomNo, Ins_ID 
                          FROM studSched 
                          WHERE SubjectCode = '" & classno1.Text & "' AND                    
                                SubjectCode = '" & classno2.Text & "' AND  
                                SubjectCode = '" & classno3.Text & "' AND  
                                SubjectCode = '" & classno4.Text & "' AND  
                                SubjectCode = '" & classno5.Text & "' AND  
                                SubjectCode = '" & classno6.Text & "' AND  
                                SubjectCode = '" & classno7.Text & "' AND  
                                SubjectCode = '" & classno8.Text & "' "

不建议这样做,使用参数化查询

Dim Cmd as New SqlCommand()
cmd.Connection = con
cmd.CommandText = "SELECT ClassNo, SubjectCode, Title, Lec,Lab, Days,TimeSlot, roomNo,Ins_ID 
                   FROM studSched WHERE SubjectCode = @Sub1 AND SubjectCode = @Sub2 AND                     
                                        SubjectCode = @Sub3 AND SubjectCode = @Sub4 AND 
                                        SubjectCode = @Sub5 AND SubjectCode = @Sub6 AND        
                                        SubjectCode = @Sub7 AND SubjectCode = @Sub8"
cmd.Parameters.AddWithValue("@Sub1", classno1.Text)
cmd.Parameters.AddWithValue("@Sub2", classno2.Text)
cmd.Parameters.AddWithValue("@Sub3", classno3.Text)
cmd.Parameters.AddWithValue("@Sub4", classno4.Text)
cmd.Parameters.AddWithValue("@Sub5", classno5.Text)
cmd.Parameters.AddWithValue("@Sub6", classno6.Text)
cmd.Parameters.AddWithValue("@Sub7", classno7.Text)
cmd.Parameters.AddWithValue("@Sub8", classno8.Text)