이 코드는 작동합니다. 그것은 인터넷에서 찾은 일부 코드를 기반으로합니다.ExecuteScalar()를 사용하는 가장 좋은 방법 찾기
코딩이 스칼라 값을 얻는 가장 좋은 방법인지 그리고 더 나은 방법으로 코딩 샘플을 보여줄 수 있는지 알려주실 수 있습니까?
Dim objParentNameFound As Object
TextBoxParentsName.Text = ""
If TextBoxParentID.Text <> "" Then
' Display the parent's name using the parent ID. '
Dim strSqlStatement As String = "Select FatherName " & _
"From Parents " & _
"Where ID = @SearchValue"
' Set up the sql command and lookup the parent. '
Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)
With objSqlCommand
' Add SqlParameters to the SqlCommand. '
.Parameters.Clear()
.Parameters.AddWithValue("@SearchValue", TextBoxParentID.Text)
' Open the SqlConnection before executing the query. '
Try
ObjConnection.Open()
Try
objParentNameFound = .ExecuteScalar()
If objParentNameFound <> Nothing Then
' Display the parent name here. '
TextBoxParentsName.Text = objParentNameFound
End If
Catch exSqlErrors As SqlException
MessageBox.Show("Sorry, I couldn't execute your query because of this error: " & _
vbCrLf & vbCrLf & exSqlErrors.Message, _
"Error")
End Try
Catch exErrors As Exception
MessageBox.Show("Sorry, there was an error. Details follow: " & _
vbCrLf & vbCrLf & exErrors.Message, _
"Error")
Finally
ObjConnection.Close()
End Try
End With
End Using
End If
감사합니다. Microsoft Access 응용 프로그램을보고 배울 수 있는지 확인합니다. 시간이 있다면 코드를 다른 방법으로 분리하는 데 도움이되는 몇 가지 코드 블록을 보여줄 수 있습니까? –