Click here to Skip to main content
15,884,473 members
Home / Discussions / C#
   

C#

 
QuestionTriangles count in Graph Pin
Member 1177893019-Nov-22 15:20
Member 1177893019-Nov-22 15:20 
AnswerRe: Triangles count in Graph Pin
Dave Kreskowiak19-Nov-22 18:03
mveDave Kreskowiak19-Nov-22 18:03 
AnswerRe: Triangles count in Graph Pin
OriginalGriff19-Nov-22 20:20
mveOriginalGriff19-Nov-22 20:20 
AnswerRe: Triangles count in Graph Pin
Gerry Schmitz20-Nov-22 5:13
mveGerry Schmitz20-Nov-22 5:13 
QuestionDynamic table insert issue with dates Pin
Vijay Bhaskar Oct202217-Nov-22 6:32
Vijay Bhaskar Oct202217-Nov-22 6:32 
AnswerRe: Dynamic table insert issue with dates Pin
Slacker00717-Nov-22 6:46
professionalSlacker00717-Nov-22 6:46 
AnswerRe: Dynamic table insert issue with dates Pin
OriginalGriff17-Nov-22 8:40
mveOriginalGriff17-Nov-22 8:40 
AnswerRe: Dynamic table insert issue with dates Pin
Richard Deeming17-Nov-22 21:37
mveRichard Deeming17-Nov-22 21:37 
As Griff said, your code is vulnerable to SQL Injection[^].

Fixing it to use parameters isn't too hard:
C#
StringBuilder sb = new StringBuilder("INSERT INTO table VALUES (");
foreach (object value in ((IDictionary<string, object>)rec).Values)
{
    if (command.Parameters.Count != 0) sb.Append(", ");
    string name = "@V" + command.Parameters.Count;
    command.Parameters.AddWithValue(name, value);
    sb.Append(name);
}
sb.Append(");");

command.CommandText = sb.ToString();

However, this may still not work. You haven't specified the list of columns you want to insert into. And there's no guarantee that the dictionary's Values collection will return the values in the same order as the columns of the table. So you could end up trying to insert the wrong value into the wrong column, which will either result in an error, or in data corruption.

Assuming the keys of your dictionary match the column names from your table, you'll want something more like this:
C#
StringBuilder columnsList = new StringBuilder();
StringBuilder valuesList = new StringBuilder();
foreach (KeyValuePair<string, object> item in (IDictionary<string, object>)rec)
{
    if (columnsList.Length != 0) columnsList.Append(", ");
    if (valuesList.Length != 0) valuesList.Append(", ");
    
    string name = "@" + item.Key;
    command.Parameters.AddWithValue(name, item.Value);
    columnsList.Append(item.Key);
    valuesList.Append(name);
}

command.CommandText = "INSERT INTO table (" + columnsList + ") VALUES (" + valuesList + ");";




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

QuestionMicrosoft.Practices.Composite.Presentation.Events Pin
Kevin Marois16-Nov-22 12:14
professionalKevin Marois16-Nov-22 12:14 
AnswerRe: Microsoft.Practices.Composite.Presentation.Events Pin
Dave Kreskowiak16-Nov-22 12:49
mveDave Kreskowiak16-Nov-22 12:49 
AnswerRe: Microsoft.Practices.Composite.Presentation.Events Pin
Richard Deeming16-Nov-22 21:31
mveRichard Deeming16-Nov-22 21:31 
QuestionI'm getting error code CS1001. And I'm not sure how to diagnose the problem. Pin
Rakos4613-Nov-22 10:46
Rakos4613-Nov-22 10:46 
AnswerRe: I'm getting error code CS1001. And I'm not sure how to diagnose the problem. Pin
Mycroft Holmes13-Nov-22 11:06
professionalMycroft Holmes13-Nov-22 11:06 
AnswerRe: I'm getting error code CS1001. And I'm not sure how to diagnose the problem. Pin
OriginalGriff13-Nov-22 18:53
mveOriginalGriff13-Nov-22 18:53 
AnswerRe: I'm getting error code CS1001. And I'm not sure how to diagnose the problem. Pin
Calin Negru14-Nov-22 3:28
Calin Negru14-Nov-22 3:28 
AnswerRe: I'm getting error code CS1001. And I'm not sure how to diagnose the problem. Pin
Victor Nijegorodov14-Nov-22 3:40
Victor Nijegorodov14-Nov-22 3:40 
QuestionMy Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Radheya Patil9-Nov-22 17:45
Radheya Patil9-Nov-22 17:45 
AnswerRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
OriginalGriff9-Nov-22 18:48
mveOriginalGriff9-Nov-22 18:48 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Radheya Patil10-Nov-22 1:05
Radheya Patil10-Nov-22 1:05 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Richard Deeming10-Nov-22 1:24
mveRichard Deeming10-Nov-22 1:24 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Radheya Patil10-Nov-22 1:42
Radheya Patil10-Nov-22 1:42 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Richard Deeming10-Nov-22 2:00
mveRichard Deeming10-Nov-22 2:00 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Radheya Patil10-Nov-22 16:37
Radheya Patil10-Nov-22 16:37 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
jsc4210-Nov-22 5:08
professionaljsc4210-Nov-22 5:08 
GeneralRe: My Code gets stuck at MessageBox.Show() after pressing OK it does not move further. Pin
Radheya Patil10-Nov-22 16:39
Radheya Patil10-Nov-22 16:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.