vb.net

2013-08-24 2 views
0

이 내 데이터베이스 테이블vb.net

ID 이름

1     ABC
2    가 나는 한 후, 각각의 값을 입력
XYZ "ABC"디스플레이에 단일 값 형태 데이터베이스를 가져 오는 다른 텍스트 상자에 ???

Private Sub butsea_Click(sender As Object, e As EventArgs) Handles butsea.Click 

    Dim dset As New DataSet 
    Dim da As SqlDataAdapter 
    Dim myCmd As New SqlCommand 

    Try 
    myConn.ConnectionString = "Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;" 
     myConn.Open() 

     Dim avalue As String = (InputBox("Input Student Id", "Search Student")).ToString 
     txt_id.Text = avalue 
     da = New SqlDataAdapter("SELECT * FROM studentdetails where student_id= '" & txt_id.Text & "", myConn) 
     If dset.Tables(0).Rows.Count > 0 Then 
      'what should i write here 
     Else 
      MsgBox("No Record Found") 
     End If 

    Catch ex As Exception 
     MsgBox(ex.Message) 
    Finally 
     myConn.Close() 
    End Try 

End Sub 

답변

1

표시 할 모든 필드에 다른 텍스트 상자를 추가해야합니다.
(예 : 이름에 txtStudentName이라는 텍스트 상자를 사용할 수 있으며 테이블 studentdetails에있는 다른 필드에 대해서도 계속 사용할 수 있습니다).

당신은 단지 하나의 값 가져 오기해야하는 경우 다음과 같은

Dim dset As New DataSet 
    Dim da As SqlDataAdapter 

Try 
    myConn.ConnectionString = "Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;" 
    myConn.Open() 
    Dim avalue As String = (InputBox("Input Student Id", "Search Student")).ToString 
    txt_id.Text = avalue 

    ' prepare the adapter with a commandtext. Do not use string concatenation from user input' 
    da = New SqlDataAdapter("SELECT * FROM studentdetails where [email protected]", myConn) 
    da.SelectCommand.Parameters.AddWithValue("@id", txt_id.Text) 

    ' Fill the dataset' 
    da.Fill(dset) 
    If dset.Tables(0).Rows.Count > 0 Then 
     ' Supposing you have a field for the studentname in the first column of the returned 
     ' datatable rows(rowindex)(columnindex) 
     txtStudentName.Txt = dset.Tables(0).Rows(0)(0).ToString() 
     ..... 
     ' Set the text property of other textboxes for other fields to show' 
    Else 
     MsgBox("No Record Found") 
    End If 
+0

답변을 위해 @steve에 감사드립니다. –

2

을해야 데이터베이스 (A SqlDataReader 개체를 사용하는 등의 다른 방법을 떠나)을 조회하는 올바른 방법 - 데이터 어댑터 및 데이터 집합이 잔인한 사용합니다. SqlCommand.ExecuteScalar 메서드를 사용하고 쿼리에서 "SELECT * ..."대신 "SELECT YOURFIELD ..."를 수행하십시오.

이 방법을 사용하면 데이터 세트를 작성하고 행 수를 확인한 다음 Nothing 대신 반환 된 값을 확인할 필요가 없습니다. 여기

UPDATE는 코드가 ExecuteScalar는

이 코드는 u를 표시해야 데이터베이스 필드가 student_name라고 가정합니다
Private Sub butsea_Click(sender As Object, e As EventArgs) Handles butsea.Click 

     Dim myCmd As SqlCommand 
     Dim myConn As New SqlConnection 
     Dim oResult As Object 

     Try 
      myConn.ConnectionString = "Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;" 
      myConn.Open() 

      Dim avalue As String = (InputBox("Input Student Id", "Search Student")).ToString 
      txt_id.Text = avalue 

      myCmd = New SqlCommand("SELECT student_name FROM studentdetails where student_id= '" & txt_id.Text & "'", myConn) 
      oResult = myCmd.ExecuteScalar() 

      If oResult IsNot Nothing Then 
       txt_name.text = oResult.ToString 
      Else 
       MsgBox("No Record Found") 
      End If 

     Catch ex As Exception 
      MsgBox(ex.Message) 
     Finally 
      myConn.Close() 
     End Try 
End Sub 

를 사용하도록 수정하면 텍스트 상자가 양식에 txt_name라고 추가 한.

+0

제안을위한 thx, 나에게 ExecuteScalar 데모를 줄 수 있습니까 ???? –

+0

답변을 코드 예제로 업데이트했습니다. 그것은 더욱 개선 될 수있다. 그것은 문자열 인라인 (Google "SQL Injection")에 쿼리 매개 변수를 추가하는 것은 좋지 않으므로 @Steve 대답은 SqlParameter를 사용하여 쿼리에 매개 변수를 추가 할 수 있음을 암시합니다. –

+0

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 다른 컬럼을 추가하면 그 값에 접근하고 싶습니다. 그러면 같은 방법을 사용할 수 있습니다. 아니면 다른 접근 방식을 쓰어야할까요? @ Yuriy Galante –