Search in SqlServer with Query

2

I have the following problem: I would like the person to type the number of ribbons in an edit and that value would be compared to the values in the database and when they are the same. present them in the report:

SQL.add('Select * from "controle" where "ribbons" = :ribbons');
FrMRelatorio.ADOQuery1.Parameters.ParamByName('ribbons').Value := edit1.Text;

With the following code the same always returns the ribbons referring to 4 Where can I be wrong?

    
asked by anonymous 21.10.2015 / 16:52

3 answers

1

You can also, instead of creating parameters, add edit1.text inside SQL, like this:

SQL.Add('Select * from controle where ribbons = '+QuotedStr(edit1.text.Text)+'');
    
22.10.2015 / 02:31
1

You should remove the double quotation marks from the controle table and the ribbons field and also specify the query parameter you are using.

It would look like this:

FrMRelatorio.ADOQuery1.SQL.add('Select * from controle where ribbons = :ribbons');
FrMRelatorio.ADOQuery1.Parameters.ParamByName('ribbons').Value := edit1.Text;

See more about how to work with parameters in the query .

    
22.10.2015 / 00:18
0

When you use parameters, you need to define them in the "Parameters" property, but when you need to make some changes you need to, for example, load the fields in your table you may have problems, you need to reset everything again in the same way as if you need to set field masks you must use the Before Open event. Usually I use (ADOQuery + DataSetProvider + ClientDataSet). I do not use parameters, I make queries using pure sql.

You can mount up a procedure so you can do all your queries, usually I create an ADOQuery for queries only as follows: (sqlGeneric + dspGeneric + cdsGeneric);

    //adicionar no type
    procedure ConsultaDadosGenerico(NomeDoClienteDataSet:TClientDataSet; sqlConsulta :String);
procedure TForm1.Button1Click(Sender: TObject);
begin
  ConsultaDadosGenerico(cdsCliente, 'Select * from controle where ribbons = ' +  QuotedStr(Trim(Edit1.Text)) );
end;

procedure TForm1.ConsultaDadosGenerico(NomeDoClienteDataSet: TClientDataSet; sqlConsulta :String);
begin
  NomeDoClienteDataSet.Close;
  NomeDoClienteDataSet.CommandText := '';
  NomeDoClienteDataSet.CommandText := sqlConsulta;
  NomeDoClienteDataSet.Open;
end;
    
01.11.2015 / 02:20