Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 lists and want to use them in mysql insert parameter:

What I have tried:

C#
List<string> ColumnNames = new List<string>();
List<string> ValueNames = new List<string>();
ColumnNames = names.Keys.ToList();
ValueNames = names.Values.ToList();

I'm trying to use this lists into MySql statement:
C#
...
MySqlCommand command = conDataBase.CreateCommand();
command.CommandText = string.Format("insert into MyTable {0} values (?parameter)", string.Join(",", ColumnNames));
command.Parameters.AddWithValue("parameter", string.Join(",", ValueNames));
...

but I'm taking syntax error, then I have tryed many experiments, like:
C#
...
command.CommandText = "insert into MyTable (" + string.Join(",", ColumnNames) + ") values ('" + string.Join(",", ValueNames) + "')";
...

and
C#
...
command.CommandText = "insert into MyTable (" + string.Join(",", ColumnNames) + ") values (?parameter)";
command.Parameters.AddWithValue("parameter", string.Join(",", ValueNames));
...

I'm taking error "Column count doesn't match value count at row 1", but
C#
ColumnNames.Count = ValueNames.Count
Posted
Updated 16-Jan-18 6:19am
v2
Comments
CHill60 15-Jan-18 8:00am    
What is the content of command.CommandText after you have done that string format and what is the content of string.Join(",", ValueNames)
Member 12361495 15-Jan-18 8:19am    
command.CommandText = insert into msoflioqveknebisklasifikatori (cifruliKodi,dasaxeleba,sruliDasaxeleba,KodiAlfa2,KodiAlfa3) values (', , , , ')
ValueNames content is array of strings

Try This :

 List<string> ColumnNames = names.Keys.ToList()
      List<string> ValueNames = names.Values.ToList();
      List<string> RevValueNames = new List<string>();
      ValueNames.ForEach(x => RevValueNames.Add("'" + x + "'"));
string Query = string.Format("insert into MyTable ({0}) values ({1})", string.Join(",", ColumnNames), string.Join(",", RevValueNames));
            command.CommandText = Query;
 
Share this answer
 
v2
Comments
Member 12361495 15-Jan-18 8:46am    
yes!!!, thank you very much
there is a little mistake instead of {0} must be ({0}).
Thank you
Altaf Ansari 15-Jan-18 8:54am    
Yes i have revised the answer..
NB: Make sure your column names cannot be provided by the user, otherwise you will leave yourself open to SQL Injection[^].

Something like this should work:
C#
command.CommandText = string.Format("INSERT INTO MyTable ({0}) VALUES (?{1})", 
    string.Join(",", names.Keys), 
    string.Join(",?", names.Keys));

// Eg: "INSERT INTO MyTable (col1, col2) VALUES (?col1, ?col2)"

foreach (KeyValuePair<string, string> pair in names)
{
    command.Parameters.AddWithValue(pair.Key, pair.Value);
}


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
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