2013-04-16 5 views
1

매개 변수없이 OnClick 버튼에서 저장 프로 시저를 어떻게 실행할 수 있습니까? 이미 연결이 그냥 당신이 당신의 SqlConnection가 평생 오픈 들고 전역 변수를 유지하지 제언 ExecuteNonQuery 방법 그러나저장 프로 시저 실행 OnClick

Protected Sub btnExport_Click(sender As Object, e As System.EventArgs) Handles btnExport.Click 

     Dim exec As SqlCommand = New SqlCommand("up_ExportFile", conn) 
     exec.CommandType = CommandType.StoredProcedure 
     exec.ExecuteNonQuery() 

End Sub 

를 추가 열었습니다 치죠

Protected Sub btnExport_Click(sender As Object, e As System.EventArgs) Handles btnExport.Click 

     Dim exec As SqlCommand = New SqlCommand("up_ExportFile", conn) 
     exec.CommandType = CommandType.StoredProcedure 

End Sub 

답변

2

: 난 뒤에 다음 코드가 귀하의 응용 프로그램의. SqlServer 용 ADO.NET 공급자는 connection pooling mechanism을 구현하므로 연결을 여는 것은 간단한 절차라는 것을 기억하십시오.
그래서 더 나은 방법이 필요하고 폐쇄와 조언에 대한 See Using Statememt

+0

감사를 배치 바로 그 때 연결 개체가 생성됩니다 이런 식으로

Using conn = new SqlConnection("your_connection_string_here") conn.Open() Dim exec As SqlCommand = New SqlCommand("up_ExportFile", conn) exec.CommandType = CommandType.StoredProcedure exec.ExecuteNonQuery() End Using 

입니다. 누락 된 exec.ExecuteNonQuery()가 트릭을 수행했습니다. 이제 실행 중입니다. 답변을 수락했습니다. –

관련 문제