如何在没有参数的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
End Sub
答案 0 :(得分:2)
假设您已经打开了连接,只需添加ExecuteNonQuery方法
即可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
但是,我建议您不要保留一个全局变量,在您的应用程序生命周期内保持SqlConnection
打开。请记住,SqlServer的ADO.NET提供程序实现了connection pooling mechanism,因此打开连接是一个轻量级的过程。
所以更好的方法是
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
通过这种方式,只在需要时创建连接对象,然后关闭并处理 See Using Statememt