0

学校のプロジェクトで何人かの患者を見つけるために検索バーを作成していますが、検索すると機能しますが、別の検索を行うと、番号が存在しても存在しないかのようにメッセージが送信されました。これはコードです。ボタンのあなたが私を助けることができることを願っています。

Private Sub cmdIDBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscarID.Click

    Dim sqlCon As New SqlClient.SqlConnection
    Dim sqlComm As New SqlClient.SqlCommand

    'Ruta de la conección.
    sqlCon.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Sistema para Hospitales.mdf;Integrated Security=True;User Instance=True")
    'Instrucción con la que se trabajara.
    sqlComm.CommandText = "SELECT * FROM [Pacientes] WHERE IDPaciente= '" & txtID.Text & "';"
    'Abrir la coneccion SQL
    sqlCon.Open()

    Do Until txtID.Text = txtCompararID.Text

        Me.PacientesBindingSource.MoveNext()

        Exit Do

        If EOF(True) Then KryptonMessageBox.Show("Error, no se encontro paciente.", "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)

    Loop

    If txtID.Text = txtCompararID.Text Then
        txtNombres.Text = txtCompararN1.Text & " " & txtCompararN2.Text & " " & txtCompararN3.Text
        txtApellidos.Text = txtCompararAp1.Text & " " & txtCompararAp2.Text
        txtEdad.Text = txtCompararEdad.Text
        Select Case txtCompararSexo.Text
            Case Is = "F"
                txtSexo.Text = "Femenino"
            Case Is = "M"
                txtSexo.Text = "Masculino"
        End Select
        Select Case TipoAfiliacionTextBox.Text
            Case Is = "1"
                txtTAfiliacion.Text = "Cotizante"
            Case Is = "2"
                txtTAfiliacion.Text = "Beneficiario"
            Case Is = "3"
                txtTAfiliacion.Text = "Pensionado"
        End Select
        txtAltura.Text = AlturaTextBox1.Text
        txtPeso.Text = PesoTextBox1.Text
        txtPresion.Text = PresionTextBox.Text
        txtTemperatura.Text = TemperaturaTextBox.Text
    Else
        KryptonMessageBox.Show("No se encontro el paciente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

End Sub
4

1 に答える 1

1

他の問題の中でも、比較ループの途中にExit Doステートメントがあるため、doループは最大1回実行されるため、おそらく最初のレコードにしか一致しません。

txtCompararIDはPacientesBindingSourceにデータバインドされており、txtIDに一致する値が見つかるまで、ループの意図はこのバインディングソースを移動すると推測しています。

その場合、doループは次のようになります。

' Get back to the top of the list
Me.PacientesBindingSource.MoveFirst()

Do Until txtID.Text = txtCompararID.Text

    Me.PacientesBindingSource.MoveNext()

    If EOF(True) Then 
       KryptonMessageBox.Show("Error, no se encontro paciente.", "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)

       Exit Do
    End If
Loop

さらに、接続オブジェクトとコマンドオブジェクトにUsingステートメントを使用して、使用が終了したときにそれらが適切に閉じられて破棄されるようにする必要があります。

例えば:

Using sqlCon As New SqlClient.SqlConnection
Using sqlComm As New SqlClient.SqlCommand

... all of your code
End Using
End Using

そして最後に、そして最も重要なこととして、値の直接入力を許可しているため、SQLインジェクション攻撃を防ぐためにパラメーター化されたクエリステートメントを使用する必要があります。この文:

sqlComm.CommandText = "SELECT * FROM [Pacientes] WHERE IDPaciente= '" & txtID.Text & "';"

次のように変更する必要があります。

sqlComm.CommandText = "SELECT * FROM [Pacientes] WHERE IDPaciente= ?"
sqlComm.Parameters.AddWithValue("IDPaciente", txtID.text)
于 2012-08-01T20:28:07.650 に答える