组合框的数据库数据? [英] Database data to combobox?
问题描述
我正在使用Microsoft Visual Studio 2005
i在MySQL中有这个表:
I am using Microsoft Visual Studio 2005
i have this table in MySQL:
RoleID Role
1 Officer
2 Advisor
3 Administrator
如何在VB.net?
How do i put Role column on my combobox in VB.net?
推荐答案
示例代码:
sample code:
Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As New MySqlConnection
'MySqlCommand It represents a SQL statement to execute against a MySQL Database
Dim cmd As New MySqlCommand
'Represents a set of data commands and a database connection that
'are used to fill a dataset and update a MySQL database. This class cannot be inherited.
Dim da As New MySqlDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = ("server=localhost;user id=root;password=;database=test")
Try
'we open Connection
con.Open()
With cmd
.Connection = con
.CommandText = "SELECT RoleID, Role from RoleTable;"
End With
'declare dt as new datatable
Dim dt As New DataTable
With ComboBox1
da.SelectCommand = cmd
'it fills the da values into dt
da.Fill(dt)
'dt provides the data surce of combobox
.DataSource = dt
'specify the what to display
.DisplayMember = "Role"
'and the value
.ValueMember = "RoleID"
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
End Class
很少ferences:
使用Visual Basic.Net中的MySQL数据库中存储的数据填充Combobox [ ^ ]
从Visual C#连接到MySQL# [ ^ ]
使用C#和.NET连接到MySQL数据库 [ ^ ]
使用VB.NET访问MySQL MySQL Connector / Net,第6部分:创建连接 [ ^ ]
使用MySQL Connector / Net在VB.NET上访问MySQL,第7部分:执行SQL操作 [ ^ ]
使用MySQL Connector / Net在VB.NET上访问MySQL,第8部分:Di GUI上的splay结果 [ ^ ]
few references:
Filling Combobox with Data Stored in MySQL Database in Visual Basic.Net[^]
Connecting to MySQL from Visual C#[^]
Connecting to MySQL Database using C# and .NET[^]
Accessing MySQL on VB.NET using MySQL Connector/Net, Part 6: Create Connection[^]
Accessing MySQL on VB.NET using MySQL Connector/Net, Part 7: Perform SQL Operations[^]
Accessing MySQL on VB.NET using MySQL Connector/Net, Part 8: Display Result on GUI[^]
试用:
Try with :
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = Connection.GetConnection();
cn.Open();
SqlDataAdapter adpt1 = new SqlDataAdapter("SELECT RoleID, Role from RoleMaster", cn);
DataTable dt1 = new DataTable();
adpt1.Fill(dt1);
//dt provides the data surce of combobox
DropDownList1.DataSource = dt
//specify the what to display
DropDownList1.DataTextField= "Role"
//and the value
DropDownList1.DataValueField= "RoleID"
DropDownList1.DataBind();
cn.Close();
}
这篇关于组合框的数据库数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!