0

我使用 vb.net。有一个小问题。我从组合框中选择哪个学生 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

如何根据数据库中的索引分配值成员。预先感谢。

4

2 回答 2

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
于 2013-05-08T22:17:23.440 回答
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 轻松检索到的重要值 id 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"
于 2013-05-08T22:12:38.957 回答