简体   繁体   中英

Pass Unknown Number of Parameters to SQL Stored Procedure

I'm a bit of a newbie when it comes to executing stored procedures through vb.net. However, I was wondering if anybody knows how I can create a function in my database class to accept SP name and an unknown amount of parameters with their associated value to execute against a SQL database.

This would enable to execute any stored procedure designed to bring back a datatable using the same function in my class.

Any help would be great. I understand that there is a SqlParamaterCollection or could I use a parameter array?

You can accomplish this by adding default values in for parameters that you may not pass in.

W3Schools has a very good explanation on doing this here

And just in case this doesn't answer your question on how to do this in vb.net, there are several ways to handle this in code. The most straightforward being an if statement.

if (there is a value to pass in then) then
     command.parameters.addwithvalue("@Parameter", your value)
endif

This is kind of what I am working on:

Public Sub CallSproc(SprocName As String, Server As String, Database As String, Optional ParameterArray(,) As String = Nothing)

Just create a two dimensional array to pass to your sub where the parameter name and value are passed. I break them apart using ubound and that will tell you how many parameters are in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM