2016-08-22 2 views
1

콤보 상자를 채우는 데 문제가 있습니다. 간단한 선택 쿼리를 하나 채울 수 있습니다. 이제 where 명령을 포함하는 쿼리로 콤보 상자를 채우고 싶습니다. 몇 가지 해결책을 시도했지만 그 중 아무 것도 효과가 없었습니다. Where 문을 사용하여 쿼리로 콤보 상자를 채울 수 있습니까?where 명령 (VB.NET)을 사용하여 쿼리를 사용하여 콤보 상자 채우기

내 코드는 지금까지 있습니다 :

Public Function vulComboboxTesten(box As ComboBox, naam As String) As ComboBox 
     box.Items.Clear() 
     box.Items.Add(" ") 
     Dim query As String 
     query = "Select Sector from Onderaannemers where Naam_firma = @naam " 
     Debug.WriteLine(query) 
     Dim command As OleDbCommand 
     command = New OleDbCommand(query, connectie) 

     command.Connection.Open() 

     Dim datareader As OleDbDataReader 
     datareader = command.ExecuteReader 

     While datareader.Read 
      Dim item As New ComboBoxItem 
      item.Content = datareader("Sector") 
      box.Items.Add(item) 
     End While 

     command.Connection.Close() 
     Return box 
    End Function 
+0

'Onderaannemers에서 섹터를 선택하십시오. 여기서 Naam_firma = @ naam'이 문제입니다. 매개 변수를 쿼리에 추가했지만 쿼리에 매개 변수를 포함시키지 마십시오 ... – Codexer

+0

어떻게 처리해야합니까? – Messiaeno

+1

다음과 같은 명령입니다 :'command.Parameters.Add (새 OleDbParameter ("@ naam", naam-yourvalue))'*** *** command = New OleDbCommand (query, connectie)'다음에 이것을하십시오. 또 다른 메모에서 데이터베이스를 때 연결을 처분하는 것이 중요합니다, 나는 처분받을 수 있도록 사용하여 성명서에 명령과 연결을 래핑하는 것이 좋습니다 것이 좋습니다. – Codexer

답변

1

이 선생님을보십시오. 난 당신의 코딩 유형에 내 코드를 번역하는 방법을 몰라. 그러나 설명을 읽으십시오.

Private Sub StoringDatainCombobox() 
    Try 
     Dim comboSource As New Dictionary(Of String, String)() 
     mkcon() 'this is just my sqlCon.Open 
     Dim cmd As New SqlCommand 
     Dim rd As SqlDataReader 

     cmd.Connection = sqlCon 
     cmd.CommandText = "data_get" 'my query stored procedure. 
     'cotains something like this select * from tb1 where isactive = true 
     cmd.CommandType = CommandType.StoredProcedure 

     comboSource.Add(0, "") ' for blank item in the 1st value of combobox 
     ComboBox1.DataSource = New BindingSource(comboSource, Nothing) 
     ComboBox1.DisplayMember = "Value" 
     ComboBox1.ValueMember = "key" 
     ComboBox1.Text = "" 

     rd = cmd.ExecuteReader 
     While (rd.Read) 
       comboSource.Add(rd("dataId"), rd("dataName")) 
       ComboBox1.DataSource = New BindingSource(comboSource, Nothing) 
       ComboBox1.DisplayMember = "Value" 
       ComboBox1.ValueMember = "key" 
       ComboBox1.Text = "" 
     End While 
     sqlCon.Close() 
    Catch ex As Exception 
     MessageBox.Show("Failed." & ex.Message, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
End Sub 

이제 ID를 사용하여 쿼리의 데이터 결과를 표시 할 수 있습니다. 사용자가 콤보 상자에서 항목을 선택했을 때 ID를 얻는 방법이 필요한 경우 알려주세요.

희망이 도움이 될 것입니다.

관련 문제