Poza możliwym wstrzykiwaniem SQL przez łączenie łańcuchów, użycie parametrów jest bardzo bliskie.
Twój ciąg zapytania zawiera (barras =„@barcodes”), usuń pojedyncze „cudzysłowy”, a parametr powinien być „kodami kreskowymi”, a nie barras. Jeśli chodzi o produkt z symbolami wieloznacznymi „%”, utwórz ciąg, który wymusza ustawienie domyślne całego parametru, który ma je uwzględnić... jak
string selectQuery =
@"select
descricao,
codigo
from
produtos
where
barras = @barcodes
or descricao like @product";
MySqlCommand command = new MySqlCommand(selectQuery, connection);
// the "@" is not required for the parameter NAMEs below, jut the string
// name of the parameter as in the query.
// Ok to use the actual text from your textbox entry here
command.Parameters.AddWithValue("barcodes", Txtcodigo.Text);
// but use the STRING created with '%' before/after created above
string parmProduct = '%' + Txtcodigo.Text.Trim() + '%';
command.Parameters.AddWithValue("product", parmProduct);
// NOW you can execute the reader and pull your data
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
DataTable dt2 = new DataTable();
dt2.Load(reader);
DataView dvDataTable = new DataView(dt2);
Txtproduto.Text = reader.GetString("descricao");