-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 に答える