Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've recently hit a dead end, I've been trying to make a MySql class that would handle the Insert, Delete, Open Connection, Close Connection etc... I made a function to return the INSERT string, the INSERT string is compiled from the pass paramaters/arguments, the dead end is that to make this work I would have to pass in 2 params arrays for the column names and the values, I know that this is not possible and can't think of any other way of doing this.
C#
private string genInsertString(string tableName, params string[] columnNames)
{
    string query = "INSERT INTO " + tableName + " (" + String.Join(", ", columnNames) +"), VALUES(" +  + ")";
    return query;
}


If you could help me I would be very grateful. Thanks
Posted
Updated 15-Aug-14 3:51am
v2
Comments
[no name] 15-Aug-14 9:45am    
Why are you using params at all?
hudec117 15-Aug-14 10:40am    
The number of columns isn't fixed so using params I'm able to put in the number of columns I want.
[no name] 15-Aug-14 11:04am    
So? You can do the same thing witha List or array.

Pass a dictionary containing the parameter names and values as key/value instead of using your current approach.
 
Share this answer
 
v2
Comments
hudec117 15-Aug-14 10:40am    
How would I handle the number of columns?
Abhinav S 15-Aug-14 12:03pm    
What you need to pass is an object of a class containing all these values.
Hoewever, you can also try Tuple - http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx.
You could also create a small class that had a public property for ParamName and another public string for ParamValue and then have it take a List<> of your class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900