简单的Simond组合框索引 [英] Simple Simond Combobox Index
问题描述
我正在尝试使用数据库表中两个字段的值填充工具栏组合框。该表包含以下数据:
DBID DB_Type
1 Oracle数据库
2 Microsoft SQL Server
3 ODBC
4 Intrabase / Firebird
5 OleDB(ADO)
6 SQLite
我正在尝试从Form_Load事件中填充组合框。
I''m trying to populate a toolbar combobox with values from two fields in a database table. The table contains the following data:
DBID DB_Type
1 Oracle Database
2 Microsoft SQL Server
3 ODBC
4 Intrabase/Firebird
5 OleDB(ADO)
6 SQLite
I''m trying to populate the combobox from the Form_Load event.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim cn As New OleDbConnection
Dim cm As OleDbCommand
Dim da As New OleDbDataAdapter
Dim strCnn As String
Dim ds As New DataSet
Dim strSQL As String
strSQL = "SELECT * FROM tblDbTypes;"
'Function to build Connection string
strCnn = CnnCtrlDB(strAppPath & "\CtrlDB.ACCDB")
cn = New OleDbConnection(strCnn)
Try
'Function to check db connection status
Dim bCnn As Boolean = DBConnectionStatus(strCnn)
If bCnn Then
cn.Open()
cm = New OleDbCommand(strSQL, cn)
da.SelectCommand = cm
da.Fill(ds)
da.Dispose()
cm.Dispose()
cn.Close()
End If
'This SHOULD set the combobox index to
'number in the DBID field in the DB - IT DON'T
tsCboDBTypes.ComboBox.ValueMember = "DBID"
'This sets the combobox's DisplayMember to the
'value contained in the Db_Type field
tsCboDBTypes.ComboBox.DisplayMember = "Db_Type"
'Binds the combobox to the DataSet
tsCboDBTypes.ComboBox.DataSource = ds.Tables(0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
当我打开表单时,例程工作到我可以看到所有的地方组合框中的六个项目。我现在遇到的问题是当我点击Oracle项目时;返回的DBID为零(0),单击Microsoft SQL Server将产生一(1),依此类推。
我知道所有集合都为零基。但我想知道的是为什么.NET没有为DBID分配索引?做事的.NET方式是不是更好?构建DataSet有什么用,但必须使用VB6中使用的ADO技术填充Combobox?我从未见过数据库表,其中零可以用作主键字段中的标识号。
提前感谢,
MRM256
When I open the form the routine works to the point where I can see all six items in the combobox. The problem I''m getting now is when I click on the Oracle item; the DBID being returned is zero(0), clicking on Microsoft SQL Server will result with a one(1), and so on.
I know all collections are zero-based. But what I want to know is why doesn''t .NET assign the index to the DBID? Isn''t the .NET way of doing things suppose to be better? What is the use of building a DataSet, but have to populate the Combobox using ADO techniques used in VB6? I have never seen a database table where a zero can be used as a identification number in a Primary Key field.
Thanks in advance,
MRM256
推荐答案
我找到了自己的答案。
I have found my own answer.
'This sets the combobox.ValueMember to the
'number in the DBID column(field) of the dataset.
'I believe the ValueMember Method replaces the
'NewIndex method in VB6.
ComboBox.ValueMember = "DBID"
当我从组合框中选择数据库类型并查看ValueMember属性时,我可以看到我需要的DBID编号。
When I selected a database type from the combobox and looked at the ValueMember property I could then see the DBID number I needed.
这篇关于简单的Simond组合框索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!