Skip to content
Advertisement

Implementing a query function with undeclared number of parameters?

I’m developing a server/client application in C#. In the earlier phases of development I was writing SQL codes each time and it caused spaghetti code. Now I’m trying to make it clean. My question is: How can I write a general query generator function with dynamic parameters?

But I want to convert it into:

Advertisement

Answer

Well, if you insist on implementing such a routine (usually we use ORM) you have to parse the commandText; the simplest (but not the best) implementation is regular expressions (we Match parameter name within commandText, then Zip it with its value from parameters):

Edit: If you want / ready to specify parameters’ names, not only values you can try Tuples: for c# 7.0+

usage

for C# 6.0-:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement