2

在 VB 2010 中,我试图在不修改现有 MySQL 表的情况下以 ProperCase 显示 DisplayMember。

我的代码片段如下所示:

 Dim sql0 As String = "SELECT business, customer_id FROM customers WHERE cab = '1'"
 Dim data0 As New MySqlDataAdapter(sql0, main.conn)
 Dim ds0 As New DataSet
 ds0.Clear()
 data0.Fill(ds0, "customers")
 cmb_company.DataSource = ds0
 cmb_company.DisplayMember = "customers.business"
 cmb_company.ValueMember = "customers.customer_id"

我试过这样的事情:

 cmb_company.DisplayMember = StrConv("customers.business_name", VbStrConv.ProperCase)

但这只会改变引号部分的大小写,而不是实际返回的值。我也尝试像这样从 MySQL 查询:

 "SELECT UPPER(business), customer_id FROM customers WHERE cab = '1'"

但这无法返回“业务”字段..

如果我的组合框有适当的案例而不改变一切,我会喜欢它。

我很感激任何建议!

4

2 回答 2

0

使用 while 循环来实现这一点似乎有点愚蠢,尤其是在使用适配器填充表之后。我想我可以跳过 .fill 并通过循环手动添加项目,同时 ProperCasing 它们。无论如何,这是我的代码:

        Dim sql1 As String = "SELECT * FROM table"
        Dim data1 As New MySqlDataAdapter(sql1, main.conn)
        Dim ds1 As New DataSet
        ds1.Clear()
        data1.Fill(ds1, "table")
        cmb_buyout.DataSource = ds1

        Dim i As Integer = 0
        While i < ds1.Tables(0).Rows.Count
            Dim name As String = StrConv(ds1.Tables(0).Rows(i).Item(1).ToString, VbStrConv.ProperCase)
            ds1.Tables(0).Rows(i).Item(1) = name
            i += 1
        End While

        cmb_buyout.DisplayMember = "table.buyoutType"
        cmb_buyout.ValueMember = "table.id"

对于任何试图做到这一点的人来说,只需更改数据库中条目的大小写可能更简单。我可能最终会这样做!

于 2012-07-17T13:26:41.450 回答
0

使用第二个,但使用这样的别名:

"SELECT UPPER(business) AS Business, customer_id FROM customers WHERE cab = '1'"

MySQL文档

可以使用 AS alias_name 为 select_expr 指定别名。别名用作表达式的列名,可用于 GROUP BY、ORDER BY 或 HAVING 子句。例如:

SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;

于 2012-07-16T21:06:16.140 回答