I am new to C# and I've written the following code. After reading through the documentation I realized that my code is supposedly vulnerable to SQL injections, because I am not using parameters for my values (as far as I understand, you could inject unwanted queries through the search.Text
). Should I even worry about it, as I am essentially locking my values inside the "" quotation marks anyway?
I found some directions here, but I can't get it to work: How to use string variable in sql statement
public void InvokeDataGridAddress()
{
switch (ComboBoxSelection.Text)
{
case "NASLOV":
comboBoxValue = "SELECT * FROM [cbu_naslovi] WHERE [ADDRESS] LIKE '%" + search.Text + "%' COLLATE Latin1_general_CI_AI";
break;
case "LASTNIK":
comboBoxValue = "SELECT [cbu_naslovi].* FROM [cbu_deli], [cbu_naslovi] WHERE [cbu_deli].LASTNIK LIKE '%" + search.Text + "%' COLLATE Latin1_general_CI_AI AND [cbu_deli].IDX = [cbu_naslovi].ID";
break;
case "OBJEKT":
comboBoxValue = "SELECT * FROM [cbu_naslovi] WHERE [SO] LIKE '%" + search.Text + "%'";
break;
case "PARCELA":
comboBoxValue = "SELECT * FROM [cbu_naslovi] WHERE [P1] LIKE '%" + search.Text + "%' OR [P2] LIKE '%" + search.Text + "%' OR [P3] LIKE '%" + search.Text + "%' OR [P4] LIKE '%" + search.Text + "%' OR [P5] LIKE '%" + search.Text + "%' OR [P6] LIKE '%" + search.Text + "%' OR [P7] LIKE '%" + search.Text + "%' OR [P8] LIKE '%" + search.Text + "%' OR [P9] LIKE '%" + search.Text + "%' OR [P10] LIKE '%" + search.Text + "%' OR [P11] LIKE '%" + search.Text + "%' OR [P12] LIKE '%" + search.Text + "%' OR [P13] LIKE '%" + search.Text + "%' OR [P14] LIKE '%" + search.Text + "%' OR [P15] LIKE '%" + search.Text + "%' OR [P16] LIKE '%" + search.Text + "%' OR [P17] LIKE '%" + search.Text + "%'";
break;
}
comboBoxValue = comboBoxValue + " ORDER BY [ULICA] ASC, [OBMOCJE] ASC, LEN ([HS]) ASC, [HS] ASC, [HID] ASC";
SqlCommand cmd = new SqlCommand
{
CommandText = comboBoxValue,
Connection = con
};
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
SqlDataAdapter da = new SqlDataAdapter(cmd);
dtAddress.Clear();
da.Fill(dtAddress);
dg_address.ItemsSource = dtAddress.DefaultView;
Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
}
EDIT: Working solution made possible by Olivier Belanger and MindSwipe. I'm also leaving a reference about how to make LIKE work with % parameters: Use of SqlParameter in SQL LIKE clause not working
public void InvokeDataGridAddress()
{
switch (ComboBoxSelection.Text)
{
case "NASLOV":
comboBoxValue = "SELECT * FROM [cbu_naslovi] WHERE [ADDRESS] LIKE @SearchText COLLATE Latin1_general_CI_AI";
break;
case "LASTNIK":
comboBoxValue = "SELECT [cbu_naslovi].* FROM [cbu_deli], [cbu_naslovi] WHERE [cbu_deli].LASTNIK LIKE @SearchText COLLATE Latin1_general_CI_AI AND [cbu_deli].IDX = [cbu_naslovi].ID";
break;
case "OBJEKT":
comboBoxValue = "SELECT * FROM [cbu_naslovi] WHERE [SO] LIKE @SearchText";
break;
case "PARCELA":
comboBoxValue = "SELECT * FROM [cbu_naslovi] WHERE [P1] LIKE @SearchText OR [P2] LIKE @SearchText OR [P3] LIKE @SearchText OR [P4] LIKE @SearchText OR [P5] LIKE @SearchText OR [P6] LIKE @SearchText OR [P7] LIKE @SearchText OR [P8] LIKE @SearchText OR [P9] LIKE @SearchText OR [P10] LIKE @SearchText OR [P11] LIKE @SearchText OR [P12] LIKE @SearchText OR [P13] LIKE @SearchText OR [P14] LIKE @SearchText OR [P15] LIKE @SearchText OR [P16] LIKE @SearchText OR [P17] LIKE @SearchText";
break;
}
comboBoxValue = comboBoxValue + " ORDER BY [ULICA] ASC, [OBMOCJE] ASC, LEN ([HS]) ASC, [HS] ASC, [HID] ASC";
SqlCommand cmd = new SqlCommand
{
CommandText = comboBoxValue,
Connection = con
};
cmd.Parameters.AddWithValue("@SearchText", '%' + search.Text + '%');
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
SqlDataAdapter da = new SqlDataAdapter(cmd);
dtAddress.Clear();
da.Fill(dtAddress);
dg_address.ItemsSource = dtAddress.DefaultView;
Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
}