is it possible to write a method which creates a method?

Posted by Alan Bennett on Stack Overflow See other posts from Stack Overflow or by Alan Bennett
Published on 2010-06-02T23:04:16Z Indexed on 2010/06/02 23:14 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

hey guys,

this might seem like a no brainer but hopefully after i explain my problem you might understand why i am asking this.

is it possible to have a method which creates a method and its arguements?

the problem:

in my current project i have to many times call different sql statements which arent all that different.

for example i have one where i inserts some new rows but only has 2 columns and another which also inserts new rows but has 12 columns.

i have created a class called utils.cs and in there i have sorted many "handy" methods, such as validation methods which check for numeric input to text boxes etc.

so i thought well instead of having sql writing methods everywhere ill make one in there and call it when i need to so i have but it currently looks like this:

public static string getInsertSQL(string tablename, string colOne, string colTwo, string colThree, string colFour, string colFive, string colSix, string colSeven, string colEight, string colNine, string colTen, string colEleven, string colTwelve,bool active, string valueOne, string valueTwo, string valueThree, string valueFour, string valueFive, string valueSix, string valueSeven, string valueEight, string valueNine, string valueTen, string valueEleven)
    {
        string strSQL = "";
        strSQL += "INSERT INTO " + tablename;
        strSQL += "(" + colOne + " " + colTwo + " " + colThree + " " + colFour + " " + colFive + " " + colSix + " " + colSeven + " " + colEight + " " + colNine + " " + colTen + " " + colEleven + " " + colTwelve + " )";
        strSQL += " values ("+active+", " + valueOne + " " + valueTwo + " " + valueThree + " " + valueFour + " " + valueFive + " " + valueSix + " " + valueSeven + " " + valueEight + " " + valueNine + " " + valueTen + " " + valueEleven + " )";

        return strSQL;
    }

as you can see thats quite a mess

so i wondered if it was at all possible to write a method which would take an arguement of how many colums needed to be inserted and then could create a method with that many arguements.

i hope you can see what i am getting at and dont just sound like a plep!

thanks in advance

© Stack Overflow or respective owner

Related posts about c#

Related posts about mysql