如何从访问数据库获取值到组合框但拆分集合(以分号分隔)?
How to get value from access database to a combobox but split the collection(delimited by semicolons)?
我有一个包含这种格式的单元格的访问数据库 Type1;Type2;Type3
我想将这些值放入组合框中,以便组合框集合如下所示:
Type1
Type2
Type3
我知道在这里获取值是我的代码:
'type_text is the name of the combobox
Dim ds As New DataSet()
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\equip_full.mdb;Jet OLEDB:Database Password="
Dim db As String = "SELECT Type FROM EquipmentType Where Equipment= '" & item_text.Text & "'"
Using cn As New OleDbConnection(ConnectionString)
Using da As New OleDbDataAdapter(db, cn)
da.Fill(ds, "Type")
End Using
End Using
With type_text
.DisplayMember = "Type"
.DataSource = ds.Tables("Type")
End With
我也知道怎么把字符串分开
Dim s As String = type_text.Text
Dim parts As String() = s.Split(New Char() {";"c})
Dim part As String
For Each part In parts
Me.type_text.Items.Add(part)
Next
如何合并这两个部分?
使用 OleDbDataReader 而不是填充 DataSet
Dim ConnectionString As String = "......."
Dim db As String = "SELECT [Type] FROM EquipmentType Where Equipment= @item"
Using cn As New OleDbConnection(ConnectionString)
Using cmd = new OleDbCommand(db, cn)
cn.Open()
cmd.Parameters.Add("@item", OleDbType.VarWChar).Value = item_text.Text
Using reader = cmd.ExecuteReader()
while reader.Read()
Dim parts As String() = reader(0).ToString().Split(New Char() {";"c})
For Each part In parts
Me.type_text.Items.Add(part)
Next
End While
End Using
End Using
End Using
另请注意,我在您名为 Type 的字段两边放置了方括号。我很确定这是一个保留关键字,因此您不能在查询文本中逐字使用它,但需要括号来帮助引擎将其识别为字段名称。如果仍然可能,最好更改该字段名。
我在您的代码中更改的另一件事是构建命令文本的字符串连接。这在 Access 中也是一个不好的做法,其中 Sql 注入有点困难。在任何情况下,使用上述参数化查询都有助于使您的命令清晰易懂,而无需提及因文本值周围缺少引号引起的错误
我有一个包含这种格式的单元格的访问数据库 Type1;Type2;Type3
我想将这些值放入组合框中,以便组合框集合如下所示:
Type1
Type2
Type3
我知道在这里获取值是我的代码:
'type_text is the name of the combobox
Dim ds As New DataSet()
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\equip_full.mdb;Jet OLEDB:Database Password="
Dim db As String = "SELECT Type FROM EquipmentType Where Equipment= '" & item_text.Text & "'"
Using cn As New OleDbConnection(ConnectionString)
Using da As New OleDbDataAdapter(db, cn)
da.Fill(ds, "Type")
End Using
End Using
With type_text
.DisplayMember = "Type"
.DataSource = ds.Tables("Type")
End With
我也知道怎么把字符串分开
Dim s As String = type_text.Text
Dim parts As String() = s.Split(New Char() {";"c})
Dim part As String
For Each part In parts
Me.type_text.Items.Add(part)
Next
如何合并这两个部分?
使用 OleDbDataReader 而不是填充 DataSet
Dim ConnectionString As String = "......."
Dim db As String = "SELECT [Type] FROM EquipmentType Where Equipment= @item"
Using cn As New OleDbConnection(ConnectionString)
Using cmd = new OleDbCommand(db, cn)
cn.Open()
cmd.Parameters.Add("@item", OleDbType.VarWChar).Value = item_text.Text
Using reader = cmd.ExecuteReader()
while reader.Read()
Dim parts As String() = reader(0).ToString().Split(New Char() {";"c})
For Each part In parts
Me.type_text.Items.Add(part)
Next
End While
End Using
End Using
End Using
另请注意,我在您名为 Type 的字段两边放置了方括号。我很确定这是一个保留关键字,因此您不能在查询文本中逐字使用它,但需要括号来帮助引擎将其识别为字段名称。如果仍然可能,最好更改该字段名。
我在您的代码中更改的另一件事是构建命令文本的字符串连接。这在 Access 中也是一个不好的做法,其中 Sql 注入有点困难。在任何情况下,使用上述参数化查询都有助于使您的命令清晰易懂,而无需提及因文本值周围缺少引号引起的错误