vb.net 从 SQL 查询将项目添加到组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15651466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add item to comboBox from SQL query
提问by Juan Filipe
hey guys I want to Add Item to my ComboBox in vb.net. But the data I need is on my database.
嘿伙计们,我想在 vb.net 中将项目添加到我的 ComboBox。但是我需要的数据在我的数据库中。
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 人变得简单!:)
采纳答案by E-r Gabriel Doronila
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..
删除行标题和列标题.. 您可以通过添加“向下箭头”按钮来复制下拉操作.. 在向下箭头按钮单击时添加一个事件.. 它应该显示数据网格视图.. 如果您单击数据网格视图上的单元格。 .您应该复制所选单元格的内容并将其复制到您的文本框,然后隐藏数据网格视图..
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
回答by user2984194
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(
回答by David Venegoni
You may have more than a single result returned from that query, but here is how to go about doing what you want:
您可能会从该查询返回多个结果,但这里是如何去做您想做的事情:
You need some sort of connection, if you don't already have one:
Dim conn As SqlConnection = New SqlConnection('connection string) conn.Open()You have the command done, now just do something like:
Dim command As SqlCommand = New SqlCommand("SELECT DISTINCT(Tags) from Legal_Records", conn)Now, you need a reader, like so:
Dim reader As SqlDataReader = command.ExecuteReader() '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 While reader.Read() ComboBox1.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 `
如果您还没有连接,则需要某种连接:
Dim conn As SqlConnection = New SqlConnection('connection string) conn.Open()您已完成命令,现在只需执行以下操作:
Dim command As SqlCommand = New SqlCommand("SELECT DISTINCT(Tags) from Legal_Records", conn)现在,您需要一个阅读器,如下所示:
Dim reader As SqlDataReader = command.ExecuteReader() '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 While reader.Read() ComboBox1.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 `
That is it, hope that helps and don't forget to close and dispose of all your resources.
就是这样,希望有所帮助,不要忘记关闭和处理您的所有资源。

