' 为存储过程设置参数
Dim prmCustomerID As New SqlParameter()
prmCustomerID.ParameterName = "@CustomerID"
prmCustomerID.SqlDbType = SqlDbType.VarChar
prmCustomerID.Size = 5
prmCustomerID.Value = "ALFKI"
cmdOrders.Parameters.Add(prmCustomerID)
Dim daGetOrders As New SqlDataAdapter(cmdOrders)
Dim dsOrders As New DataSet()
daGetOrders.Fill(dsOrders, "Orders")
DataGrid1.DataSource = dsOrders.Tables("Orders")
此代码与上一个示例中的代码非常相似,不同之处在于创建 Command 对象之后,为其配置了 Parameter 对象并将此对象添加到 Command 的参数集合中。在此示例中(更接近于演示软件)将对客户 ID 进行硬编码,参数的 Value 属性通常会设置为某些用户输入数据。但是,参数的其他属性可以完全象此示例中那样设置。
此示例中的所有参数设置都是显式设置。某些开发人员喜欢这种样式,因为它便于说明。但某些开发人员喜欢使用代码行较少的等价方法:
