Autor Beitrag
dark-destination1988
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Do 06.10.11 08:44 
Hallo,
Ich habe ein Oracle Select-Statement. Ich will nun über ein OracleCommand den Select realisieren.
Bis dato kein Problem. Jetzt habe ich aber für dieses statement eine dynamische anzahl von parametern.
Ich verwende dazu den oracle operator "in".

Hier ein beispiel
ausblenden SQL-Anweisung
1:
select * from beispiel where id in (:p1,:p2,:p3....pn)					

wobei p1...n dynamisch ist.

kann man das dynamisch gestalten, ohne den command sql string verändern zu müssen
d.h ich übergebe eine liste mit strings (also die parameter), und diese können mal 5 und mal 6,mal 10, mal 2 parameter enthalten.

Moderiert von user profile iconTh69: SQL-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4799
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 06.10.11 10:15 
Hallo,

da du jeden einzelnen Parameter eigens angeben mußt (leider erlauben die SQL-Parameter keine Liste), so mußt du eine Mischung aus String-Konkatenation und SQL-Parameter verwenden, s. z.B. Using Parameters with an SQL IN Clause

Alternativ kannst du auch einfach LINQ-to-SQL benutzen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
var ids = new [] { 12345 }; // bzw. dynamisch über eine Schleife

var results = from ex in Beispiel
              where ids.Contains(ex.Id)
              select ex;

Intern erzeugt der Linq-to-SQL Provider daraus dann ein "IN"-Statement (kannst du z.B. mit LINQPad nachschauen).
dark-destination1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Do 06.10.11 11:29 
das war meine ursprüngliche lösung und ich wollte es halt als prepared statement...
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4799
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 06.10.11 11:50 
Hallo,

welche Lösung meinst du: die aus dem verlinkten Artikel oder die LINQ-to-SQL?

Eine andere Lösung gibt es (m.E.) nicht.
dark-destination1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 178
Erhaltene Danke: 21



BeitragVerfasst: Do 06.10.11 11:57 
die mit der string konkation, ich hatte es vorher so ähnlich gemacht... aber meiner meinung nach ist das relativ unsicher