简体   繁体   English

从SQL查询将项目添加到comboBox

[英]Add item to comboBox from SQL query

hey guys I want to Add Item to my ComboBox in vb.net. 嗨,我想在vb.net中将项目添加到我的ComboBox中。 But the data I need is on my database. 但是我需要的数据在我的数据库中。

I have here a SQL Query statement : 我在这里有一个SQL查询语句:

"SELECT DISTINCT(Tags) from Legal_Records"

what I want to do is to add the Result of this SQL Query to my ComboBox. 我想做的就是将此SQL查询的结果添加到我的ComboBox中。

anyone can help me with this, make it simple tnx guys! 任何人都可以帮助我,让它成为简单的tnx家伙! :) :)

you can make a datagridview look like a combobox.. 您可以使datagridview看起来像一个组合框。

remove the rowheaders and column headers.. you can replicate the dropdown action by adding an "arrow down" button.. add an event on arrowdown button click.. it should show the datagridview.. and if you click a cell on the datagridview.. you should copy the contents of the chosen cell and copy it to your textbox, after that hide the datagridview.. 删除行标题和列标题..您可以通过添加“向下箭头”按钮来复制下拉操作。.单击箭头按钮时添加一个事件..它应显示datagridview ..如果您单击datagridview上的单元格。 。您应该复制所选单元格的内容,然后将其复制到您的文本框,然后隐藏datagridview。

it takes only 3 controls* and with that you can easily manage your datasource (using datagridview).. 它只需要3个控件*,您就可以轻松地管理数据源(使用datagridview)。

* (1)textbox and (2)arrow button down - to make it look like a combobox.. last is the (3)datagridview

You may have more than a single result returned from that query, but here is how to go about doing what you want: 从该查询返回的结果可能不止一个,但以下是执行所需操作的方法:

  1. You need some sort of connection, if you don't already have one: 如果还没有连接,则需要某种连接:

     Dim conn As SqlConnection = New SqlConnection('connection string) Dim conn As SqlConnection =新的SqlConnection('连接字符串)    \nconn.Open() conn.Open() 
  2. You have the command done, now just do something like: 您已完成命令,现在只需执行以下操作:

     Dim command As SqlCommand = New SqlCommand("SELECT DISTINCT(Tags) from Legal_Records", conn) Dim命令,如SqlCommand = New SqlCommand(“从Legal_Records中选择SELECTDISTINCT(Tags)”,conn) 
  3. Now, you need a reader, like so: 现在,您需要一个读者,像这样:

     Dim reader As SqlDataReader = command.ExecuteReader() 昏暗的阅读器为SqlDataReader = command.ExecuteReader()                   \n'your query will return an array of objects, unless you know there will be only one value to return, if that is the case, you can use command.ExecuteScalar() which will return only the first column of the first row '您的查询将返回一个对象数组,除非您知道将仅返回一个值,如果是这样,则可以使用command.ExecuteScalar(),该命令将仅返回第一行的第一列\n\nWhile reader.Read() 而reader.Read()  \nComboBox1.Items.Add(reader.GetString(0)) 'You can also do GetInt32, GetDouble, etc. Everytime you call GetString or whatever you chooose, it will move to the next item in the object array ` ComboBox1.Items.Add(reader.GetString(0))'您也可以执行GetInt32,GetDouble等。每次调用GetString或任何选择时,它将移至对象数组`中的下一项。 

That is it, hope that helps and don't forget to close and dispose of all your resources. 就是这样,希望对您有所帮助,并且不要忘记关闭和处置所有资源。

here is my sample 这是我的样品

    cbsection.Items.Clear()
    strsql = "select DISTINCT(section)  from sassign where grade like @field1"
    sqlcmd = New SqlClient.SqlCommand(strsql, sqlconn)
    With sqlcmd
        .Parameters.AddWithValue("@field1", cbgrade.Text)
    End With
    sqldr = sqlcmd.ExecuteReader
    While (sqldr.Read())
        With cbsection.Items.Add(sqldr("section"))
        End With
    End While
    sqlcmd.Dispose()
    sqldr.Close(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM