显示多个值成员

时间:2013-05-08 21:55:01

标签: mysql vb.net

我使用的是vb.net。 遇到了一个小问题。 我从组合框中选择了student_id然后我需要另一个文本框根据所选的id更改数据,但问题是值成员只读取一个值成员。

这里是我的代码:

    Dim dastudent As New Odbc.OdbcDataAdapter("SELECT * from student ", myconnection)
    Dim dsstudent As New DataSet

    'Load data about student id into the combo box
    dastudent.Fill(dsstudent, "student")
    cboID.DataSource = dsstudent.Tables("student")
    cboID.DisplayMember = "Student_Id"
    cboID.ValueMember = "Student_Name"
    cboID.ValueMember = "Student_Tel_No"
    cboID.ValueMember = "Student_Address"
    cboID.ValueMember = "Mentor_Name"
End Sub

Private Sub cboID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboID.SelectedIndexChanged
    Dim dastudent As New Odbc.OdbcDataAdapter("SELECT * from student  WHERE (Student_Id= '" & cboID.Text & "')", myconnection)

    txtName.Text = cboID.SelectedValue.ToString()
    txtTelNo.Text = cboID.SelectedValue.ToString()
    lboAddress.Text = cboID.SelectedValue.ToString()
    txtMentor.Text = cboID.SelectedValue.ToString()
End Sub

如何根据数据库中的索引分配值成员。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

我强烈建议你开始学习如何使用课程,这是一个很好的例子。您可以查询学生并将其存储以供未来使用。

快速回答。以下是您需要做的一个示例。

    Dim dastudent As New Odbc.OdbcDataAdapter("SELECT * from student ", myconnection)
    Dim dsstudent As New DataSet

    'Load data about student id into the combo box
    dastudent.Fill(dsstudent, "student")
    cboID.DataSource = dsstudent.Tables("student")
    cboID.DisplayMember = "Student_Id"
    cboID.ValueMember = "Student_Id"
End Sub

Private Sub cboID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboID.SelectedIndexChanged
    Dim dastudent As New Odbc.OdbcDataAdapter("SELECT * from student  WHERE (Student_Id= '" & cboID.SelectedValue.ToString() & "')", myconnection)

 Dim dsstudent As New DataSet

'Load data about student id into the combo box
dastudent.Fill(dsstudent, "student")

txtName.Text = dsstudent.Tables("student").Rows(dsstudent.Tables("student").Columns("Student_Name").Ordinal).ToString()
txtTelNo.Text = dsstudent.Tables("student").Rows(dsstudent.Tables("student").Columns("Student_Tel_No").Ordinal).ToString()
lboAddress.Text = dsstudent.Tables("student").Rows(dsstudent.Tables("student").Columns("Student_Address").Ordinal).ToString()
txtMentor.Text = dsstudent.Tables("student").Rows(dsstudent.Tables("student").Columns("Mentor_Name").Ordinal).ToString()
End Sub

答案 1 :(得分:0)

我将尝试使用从数据表中提取的各个部分构建一个字符串,并将此组合用作ComboBox的DisplayMember,将Student_ID作为ValueMember

Dim sqlSelect = "SELECT Student_Name || ', ' || Student_Tel_No || ', ' || Student_Address" + 
                " || ', ' || Mentor_Name As StudentInfo, Student_ID from student"
Dim dastudent As New Odbc.OdbcDataAdapter(sqlSelect, myconnection)
Dim dsstudent As New DataSet

'Load data about student id into the combo box'
dastudent.Fill(dsstudent, "student")
cboID.DataSource = dsstudent.Tables("student")
cboID.DisplayMember = "StudentInfo"
cboID.ValueMember = "Student_ID"

然后在SelectedIndexChange事件中,您可以拆分DisplayMember并提取子部分

if cboID.SelectedItem Is Not Nothing then
    Dim row = CType(cboID.SelectedItem, DataRowView)
    Dim parts = row.Item("StudentInfo").ToString().Split(",")
    txtName.Text = parts(0).Trim()
    txtTelNo.Text = parts(1).Trim()
    lboAddress.Text = parts(2).Trim()
    txtMentor.Text = parts(3).Trim()
End If

当然,如果你需要对数据库进行研究,那么重要的值就是可以从ValueMember中轻松检索到的Student_ID

Dim studID = Convert.ToInt32(cboID.SelectedValue)

忘了说运营商||适用于MySQL,但您需要将sql_mode设置为PIPES_AS_CONCAT 否则你需要CONCAT功能

"SELECT CONCAT(Student_Name, ', ', Student_Tel_No, ', ', Student_Address, " + 
                    "', ', Mentor_Name) As StudentInfo, Student_ID from student"
相关问题