Solved

passing an array as a parameter to a sqlDataAdapter

Posted on 2003-10-27
5
857 Views
Last Modified: 2012-06-22
I have a dataadapter in a data class with a sql statement like this.
Select * from Customers where CustomerID = @ CustomerID

In my Code I am passing the parameter to the dataadapter like this.
OrdersData.sqlDaInvoiceByID.SelectCommand.Parameters["@CustomerID"].Value = paramValue;

I have an array with multiple customerIDs in it.  is there anyway to pass this array as the Value.  I tried just setting paramValue = myArray put it said input sting not in correct format.

Any ideas?
0
Comment
Question by:lyptus
5 Comments
 
LVL 1

Expert Comment

by:jlach
ID: 9630146
I do not know how to do that, but i do know that you can create your sql at run time, and just execute the command... like

string ids = "0";
foreach(string str in Collection.Item) {
   ids += "," + str;
}
string sql = "select * from Customers where CustomerID in (" + ids + ")";

Then execute that sql..
0
 

Author Comment

by:lyptus
ID: 9630159
The command is in a class which I don't have access to.
0
 
LVL 1

Expert Comment

by:jlach
ID: 9630200
Then no, i dont think there is way to do this.. i dont use the whole adapter thing for paramaters and such.. do you have access to inherit anything from the class?
0
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 9635444
Hi

If the parameter is for example some string like 'a,b,c,d' and you want to use that values in the stored procedure then.
I think you have to choices.
a) To transform the parameter in the stored procedure by using substrings, or pathindex function in order to extract the elements of the array. In this way you could use some separators like the character '~' or any other separator character.

b) Other way to do this is by using directly the parameter, something like this.

Procedure x ...

@param1 varchar(100)

begin
 ... something ...

  eval('Select Field From Table Where Id in (''' + @param1 + ''' ) ')

.. something...

end

Hope helps.
0
 
LVL 2

Accepted Solution

by:
doryllis earned 250 total points
ID: 9646306
Looking at the whole thing, if you do not have access to the sql command to change it from an equal sign to a LIKE or IN, then it won't help to do just the list of params into a string.  

If you can't change the query, one alternative is to call the sql query for each item in your array and gather those results together.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question