我的kriteria
数据库的列名kode
包含kriteria 1
到kriteria 6
的值{/ p>
我希望用户可以从kode
中选择3到6个值,它会在datagridview中显示为矩阵......
如果用户选择6 kriteria,则没有问题,它显示矩阵6x6
...但如果用户选择少于6个kriteria或参数为空,则不显示任何内容......
这是我的代码
Private Sub perhitungankriteria_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In menuuser.ListBox2.Items
list2.Add(item)
Next
connect()
End Sub
Sub connect()
Dim adapter As OleDbDataReader
Dim cmd As OleDbCommand
cmd = New OleDbCommand("SELECT kode FROM kriteria WHERE kode=@kriteria1 OR kode=@kriteria2 OR kode=@kriteria3 OR kode=@kriteria4 OR kode=@kriteria5 OR kode=@kriteria6", konek)
cmd.Parameters.Add("@kriteria1", OleDbType.VarChar, 50).Value = list2(0)
cmd.Parameters.Add("@kriteria2", OleDbType.VarChar, 50).Value = list2(1)
cmd.Parameters.Add("@kriteria3", OleDbType.VarChar, 50).Value = list2(2)
cmd.Parameters.Add("@kriteria4", OleDbType.VarChar, 50).Value = list2(3)
cmd.Parameters.Add("@kriteria5", OleDbType.VarChar, 50).Value = list2(4)
cmd.Parameters.Add("@kriteria6", OleDbType.VarChar, 50).Value = list2(5)
adapter = cmd.ExecuteReader
DataGridView1.Columns.Clear()
While adapter.Read
list.Add(adapter("kode"))
DataGridView1.Columns.Add(adapter("kode"), adapter("kode"))
End While
我在sql SELECT语句中尝试IN而不是WHERE查询但是出现了同样的问题
答案 0 :(得分:0)
在用户从列表框中进行选择后,从按钮单击调用BuildCommand。
Dim list2() As String = {"kriteria1", "kriteria2", "kriteria3", "kriteria4", "kriteria5", "kriteria6"}
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = list2
Private Sub BuildCommand()
Dim sb As New StringBuilder("SELECT kode FROM kriteria WHERE kode = ")
Dim ct As Integer = ListBox1.SelectedItems.Count
Dim cn As New OleDbConnection("Your connection string")
Dim cmd As New OleDbCommand
cmd.Parameters.Add("@kriteria1", OleDbType.VarChar, 50).Value = list2(0)
cmd.Parameters.Add("@kriteria2", OleDbType.VarChar, 50).Value = list2(1)
cmd.Parameters.Add("@kriteria3", OleDbType.VarChar, 50).Value = list2(2)
cmd.Parameters.Add("@kriteria4", OleDbType.VarChar, 50).Value = list2(3)
cmd.Parameters.Add("@kriteria5", OleDbType.VarChar, 50).Value = list2(4)
cmd.Parameters.Add("@kriteria6", OleDbType.VarChar, 50).Value = list2(5)
If ListBox1.SelectedItems.Contains(ListBox1.Items(0)) Then
sb.Append("@kriteria1 OR kode = ")
End If
If ListBox1.SelectedItems.Contains(ListBox1.Items(1)) Then
sb.Append("@kriteria2 OR kode = ")
End If
If ListBox1.SelectedItems.Contains(ListBox1.Items(2)) Then
sb.Append("@kriteria3 OR kode = ")
End If
If ListBox1.SelectedItems.Contains(ListBox1.Items(3)) Then
sb.Append("@kriteria4 OR kode = ")
End If
If ListBox1.SelectedItems.Contains(ListBox1.Items(4)) Then
sb.Append("@kriteria5 OR kode = ")
End If
If ListBox1.SelectedItems.Contains(ListBox1.Items(5)) Then
sb.Append("@kriteria6;")
End If
Dim strSQL As String = sb.ToString
If strSQL.EndsWith("= ") Then
Dim index As Integer = strSQL.LastIndexOf("O") 'last capital O
strSQL = strSQL.Remove(index)
strSQL &= ";"
End If
Debug.Print(strSQL) 'Check the immediate window
cmd.CommandText = strSQL
cmd.Connection = cn
'use your command as you wish
End Sub