2016-07-26 5 views
-2

요약하면 SQL 데이터베이스에 항목을 추가/편집하는 프로그램을 작성하고 있습니다.VB : SQL 쿼리 결과를 텍스트 상자

이 프로그램의 특징 중 하나는 계정 ID 번호가 주어지면 해당 계정으로 해당 ID로 이름을 조회한다는 것입니다. 이것이 내가 문제가되는 것입니다.

일반 형식 :

  • 목적 : SQL 쿼리 문자열을 반환합니다 계좌 번호와 테이블에
  • AcctID => 필드에 텍스트 박스
  • 계정 이름과 테이블에
  • AcctName => 필드
  • txtbx_accountName => textbox 이름을 반환해야합니다.

참고 :

  • 이것은 모두 일반적인 Try-Catch 문에 처리와 함께 중첩되어 있습니다.
  • 이것은 모두 단추의 Click 이벤트 처리기 안에 있습니다.
  • 이 모두 2015
Dim myConn As New SqlConnection 
Dim myCmd As New SqlCommand 

myConn.ConnectionString = "" 
myConn.Open() ' Open the connection 
myCmd = myConn.CreateCommand() 

' Build the query with the account number as paramter 
myCmd.CommandText = "SELECT AcctName FROM DataSetTable WHERE (AcctID = @incomingAcctID)" 
' Add the parameter so the SqlCommand can build the final query 
myCmd.Parameters.Add(New SqlParameter("@incomingAcctID", (CInt(txtbx_accountNum.Text)))) 

' run the query and obtain a reader to get the results 
Dim reader As SqlDataReader = myCmd.ExecuteReader() 

' check if there are results 
If (reader.Read()) Then 
    ' populate the values of the controls 
    txtbx_accountName.Text = reader(0) 
End If 

' Close all connections 
myCmd.Dispose() 
myConn.Close() ' Close connection 
myConn.Dispose() 
+1

을 향후 참고 : 당신이 가지고있는 "문제"를 설명하십시오 - 당신이 일어나고보고 싶지 않는 무엇을 (이 경우 채워진 텍스트 상자)와 무엇을 * 보았습니까? –

+0

위의 내용을 오류 처리와 함께 일반적인 Try-Catch 문에 넣었으므로 "연결 속성이 초기화되지 않았습니다"및 "초기화 문자열의 형식이 인덱스 0에서 시작하는 사양을 따르지 않습니다"와 유사한 오류가 발생하지만 이들은 연결 문자열에 어떤 변경을했는지에 따라 다양합니다. 내가 지금 기억할 수없는 변화. 요점은 내가 아래에서 제공 한 답변이므로 "다운 투표"하지 마십시오. – eschirf

답변

-1

내가 더미 해요 비주얼 스튜디오에서 이루어집니다!

모든 이해 관계자를위한 솔루션입니다.

이벤트 처리기 (w/중첩 시도 - 캐치)를 클릭

txtbx_accountName.Text = DataSetTableAdapter.SearchNameQuery(CInt(txtbx_accountNum.Text)).ToString 

SearchNameQuery이에

SELECT AcctName FROM DataSetTable WHERE AcctID = @incomingAcctID 

더 노트 : - 데이터 집합이 프로젝트에 이미 포함되어

0

저는 프로가 아니지만이게 맘에 드는데 죄송합니다.

당신의 저장 프로 시저를 추가하고이 쓰기보다 편집하고 추가 할 곳에 전화를 만든 경우 :

private sub NAME(ByVAL Parameter1 name as integer, 
        ByVAL Parameter2 name as string, 
        ByVAL Parameter3 name as boolean) 

      Dim strConn As String = ConfigurationManager.ConnectionStrings("databaseXYZ").ConnectionString 
      Dim myConn As New SqlConnection(strConn) 
      Dim myCmd As SqlCommand 

    try 
      myConn.Open() 
      sqlCommand = New SqlCommand("PRCEDURE_NAME", myConn) 
      sqlCommand.CommandType = CommandType.StoredProcedure 

      dim param as new System.Data.SqlClient.SqlParameter 
      param.parameterName="@send_parameter1" 
      param.Direction = ParameterDirection.Input 
      param.Value = send_parameter1 


      dim param1 as new System.Data.SqlClient.SqlParameter 
      param1.parameterName="@send_parameter2" 
      param1.Direction = ParameterDirection.Input 
      param1.Value = send_parameter2 

      sqlCommand.Parameters.Add(Param) 
      sqlCommand.Parameters.Add(Param1) 
      sqlCommand.ExecuteNonQuery() 
    catch ex as exception 
     throw 
    End Try 
     myConn.Close() 
     myConn = Nothing 
    end sub 
관련 문제