-2

我在 VB.NET 中做一个项目。我有一个表单,其中有 3 个文本框和一个命令按钮。

当我在 TextBox1 中输入一个值(即患者的注册号)并单击命令按钮 (SearchButton) 时,它必须从我的 SQL Server 表中搜索该值并在 TextBox2 和 TextBox3 中给出结果(即名称和患者年龄)。

这就是我所做的,但它不起作用。

    Dim cn As New SqlConnection
    cn.ConnectionString = "Data source=localhost\SQLEXPRESS;Initial Catalog=hms;Integrated Security=True"
    cn.Open()
    Dim cm As New SqlCommand
    cm.CommandText = "SELECT Patient_Name,Age FROM Patient_Prescrib"
    cm.Connection = cn
    Dim dr As SqlDataReader
    dr = cm.ExecuteReader

    If dr.HasRows Then

        dr.Read()
        ' TextBox1.Text = dr.Item("Reg_No")
        TextBox3.Text = dr.Item("Patient_Name")
        TextBox4.Text = dr.Item("Age")
        dr.Close()

    End If
    cn.Close()
4

1 回答 1

0

@SoftwareCarpenter,@D_Bester,@rheitzman 谢谢。这就是我所做的并且做对了。:)

 Private Sub DetailsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailsButton.Click
    Try
        Dim con As SqlConnection = New SqlConnection
        con.ConnectionString = "Data source=localhost\SQLEXPRESS;Initial Catalog=hms;Integrated Security=True"
        con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New SqlDataAdapter
        da = New SqlDataAdapter("SELECT * from Patient_Prescrib WHERE Reg_No LIKE '%" & TextBox1.Text & "%'", con)
        da.Fill(dt)
        For Each DataRow In dt.Rows
            If TextBox1.Text = dt.Rows(0)("Reg_No").ToString Then

                TextBox2.Text = dt.Rows(0)("Patient_Name").ToString
                TextBox3.Text = dt.Rows(0)("Patient_Con").ToString

                MessageBox.Show("Patient details added!")
                Return
            End If
        Next
        con.Open()
        Return
        con.Close()
    Catch ex As Exception
        MessageBox.Show(" a run time error has occured. Please make sure you have provided the reg no for search")

    End Try

End Sub
于 2013-08-02T15:28:43.143 回答