此示例显示如何传递参数和执行存储过程。
示例
Dim SprocCommand As New SqlClient.SqlCommand SprocCommand.Connection = dcNorthwind SprocCommand.CommandType = CommandType.StoredProcedure SprocCommand.CommandText = "CustOrderHist" Dim CustomerIDParam As New SqlClient.SqlParameter("@CustomerID", TextBox1.Text) SprocCommand.Parameters.Add(CustomerIdParam) Dim SprocResults As SqlClient.SqlDataReader Try dcNorthwind.Open() SprocResults = SprocCommand.ExecuteReader() ' Process SprocResults datareader here. Catch ex As Exception MessageBox.Show("Failed to execute command") Finally dcNorthwind.Close() End Try
编译代码
本示例需要:
- 对 System 和 System.Data 命名空间的引用。
- 与 Northwind 示例数据库之间的数据连接 dcNorthwind。
- Northwind 示例数据库附带的 CustOrderHist 存储过程。