Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET TableAdapter parameters

I have a query that I wish to run through an ASP.NET TableAdapter that contains an 'IN' clause which is to receive it's values through a parameter.

My question is, how do I specify this parameter? I thought of writing the conditional statement like this:

AND b.group_category_id in (@ParamList)

where the @ParamList is a String of the parameters, e.g. "4,12,45,23" but since the specified id is an Integer, it complains that it cannot convert String to Integer. This makes sense, but is there any way to specify such a list in a SQL statement in an ASP.NET TableAdapter?

like image 891
Ian Devlin Avatar asked Mar 16 '09 10:03

Ian Devlin


People also ask

How do I add a parameter to a DataSet?

In the data workbench window for the Log Processing or Transformation Dataset Include file, right-click Parameters, then click Add new > Parameter. Select String Parameter, Numeric Parameter, or Vector Parameter, and complete the Name and Value parameters as described in the following sections.

What does SqlDataAdapter fill do?

The Fill method of the DataAdapter is used to populate a DataSet with the results of the SelectCommand of the DataAdapter . Fill takes as its arguments a DataSet to be populated, and a DataTable object, or the name of the DataTable to be filled with the rows returned from the SelectCommand .


2 Answers

You might have a look at http://dotnet.org.za/johanvw/archive/2008/06/13/mssql-split-function.aspx and pass it indeed as a string. Kind of a workaround than a solution. But that would only be usable if you use MSSQL.

like image 81
Sascha Avatar answered Oct 30 '22 12:10

Sascha


One workaround I've seen:

WHERE charindex(',' + cast(b.group_category_id as varchar) + ',', @ParamList) > 0

In this case the @ParamList would be a string in the form ",4,12,45,23,"

It's not "pretty", but preserves the parameter and benefits of a compiled query. Since you are searching for numbers, the sub-string guarantees a unique match.

like image 30
Ruslan Avatar answered Oct 30 '22 14:10

Ruslan