Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / September 2005

Tip: Looking for answers? Try searching our database.

SqlParameter for IN-Clausel (int values)?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roland Müller - 29 Sep 2005 15:51 GMT
Hi Ng,

i have a sucking problem......

select * from test where nId IN (1, 2, 3)

Now i want to execute this with an sqlcommand and more interesting with
an sqlParameter:

sqlCommand.CommandText = select * from test where nId in (@IN-Values)";

I first tried:

SqlParameter sqlParameter = new SqlParameter("@Values", "1, 2, 3");

Exeption: Syntax error converting the nvarchar value '1, 2, 3' to a
column of data type int

This is very logical. But how can i do?

I then tried to use an int32 array or an arraylist with int values:

int[] test = new Int32[] {1, 2, 3};
ArrayList test2 = new ArrayList();
test2.Add(1);
test2.Add(2);

SqlParameter sqlParameter = new SqlParameter("@Values", test);
SqlParameter sqlParameter = new SqlParameter("@Values", test2);

This didn't work, too. I cannot give an array or arraylist to a
parameter as value.

How can i resolve my problem? I don't want to do without sqlparameter
and i only want ONE sqlparameter! Is it impossible?

Thanks,
Roland
Kerry Moorman - 29 Sep 2005 17:43 GMT
Roland,

http://support.microsoft.com/default.aspx?scid=kb;en-us;555167

Kerry Moorman

> Hi Ng,
>
[quoted text clipped - 34 lines]
> Thanks,
> Roland
john wright - 29 Sep 2005 18:25 GMT
An easier way to do this is write a stored procedure that uses the fn_split
function.  Go online and look for the fn_split function, put it in your
master table and use it.  This will allow you to pass in an IN parameter or
any length and have it work.  This is a glaring oversite on SQL Server part.
The link here is complicated.

John
> Roland,
>
[quoted text clipped - 40 lines]
>> Thanks,
>> Roland
rviray - 29 Sep 2005 17:53 GMT
please excuse my ignorance...in this scenario what is using
SQLParameter buying you? I mean, you are building the CommandText via
string? right?

So, why not just do

string infoBegin = "Select * from test where nID in(";
string info = ""
string infoEnd = ")"

info = info + <logic to build 1,2,3>

sqlCommand.CommandText = infoBegin + info + infoEnd;

If you want to use a Sql Store Proc to accomplish this, post back and
can give you some code..

--
rvira
Roland Müller - 30 Sep 2005 07:36 GMT
Not really. There is a class with constant strings containing
sqlcommands; the are used from different places in a program or even in
different programs.
I take the contants and must fill them with MY current values
(parameters); i cannot easily modify the command text.

> please excuse my ignorance...in this scenario what is using a
> SQLParameter buying you? I mean, you are building the CommandText via a
> string? right?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.