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?