0

I have a grid that connected to AdoDataset. i want read records from two table with join SQL, but save record in a table.

Read data:

adodataset.commandtext := 'select * from Table1 left join Table2 on Table1.ID = Table2.ID';
adodataset.Open;

I want save Table1 Fields only when post

Progman
  • 16,827
  • 6
  • 33
  • 48
  • What is the problem you have with the code you show? Do you get build errors? Wrong output? Crashes? Something else? Please read the [help pages](http://stackoverflow.com/help), take the [SO tour](https://stackoverflow.com/tour), read [How to Ask](https://stackoverflow.com/questions/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to edit your questions to improve them. – fpiette Jul 19 '21 at 16:14
  • You're only selecting fields from Table1 (`select * from table1`), so I don't understand your question. You read the data from the dataset and write it to the new one just like you would any other data. There are tutorials about working with databases in Delphi available in many places on the web, and there is also a tutorial in the Delphi help file. – Ken White Jul 19 '21 at 16:45
  • In addition to what @KenWhite says, consider populating the new table entirely in SQL using SELECT INTO or INSERT Into (see https://stackoverflow.com/questions/6947983/insert-into-vs-select-into). – MartynA Jul 20 '21 at 07:53

3 Answers3

1

You can use ReadOnly property of DBGrid.Columns. For example if you have a query like this:

ADODataSet1.CommandText := 'SELECT * FROM Table1 JOIN Table2 ON Table1.ID = Table2.ID';

Then your DBGrid will be like:

enter image description here

You can then make 3rd and 4th columns read only at design time or at run time by these codes:

  DBGrid1.Columns[2].ReadOnly := True;
  DBGrid1.Columns[3].ReadOnly := True;

Also note that if you want to delete records only from Table1 then you should run this code when ADODataSet1 is active:

ADODataSet1.Properties['Unique Table'].Value := 'Table1';

Update:

As suggested in comments it's a good idea to set desired fields ReadOnly at DataSet level:

  ADODataSet1.FieldByName('ID_1').ReadOnly := True;
  ADODataSet1.FieldByName('Table2_Value').ReadOnly := True;
hsn
  • 368
  • 2
  • 9
  • You should *never* attempt to set properties of a SQL result-set by setting properties of a DBGrid. – MartynA Jul 20 '21 at 07:48
  • But what useful effect do you think you are achieving by setting those fields to read-only and exactly when do you propose to do it? – MartynA Jul 20 '21 at 08:14
  • @MartynA As I understood: OP wants only Table1.Fields to be editable but not Table2.Fields. So I didn't find any better solution for this by using TADODataSet – hsn Jul 20 '21 at 08:29
  • No, the OP said "I want save Table1 Fields only when post", not "I want only Table 1 fields to be editable", not the same thing at all. – MartynA Jul 20 '21 at 09:14
1

First of all the query example in the commandtext is wrong / ambiguous. I don't know what you want to perform but I'm guessing you want to change/save data in a table. In this case, why don't you perform the update in sql? Let's say you have the primary key field called "id", and you want to save a field called "name" with another value.

var 
  id, NewName: string;
//...
begin
  //...
  id := adodataset.fieldbyname('id').AsString; 
  adodataset.connection.execute(
    'UPDATE table1 SET name = ' + quotedstr(NewName) + ' WHERE id = ' + quotedstr(id)
  ); 
  // refresh the records by closing and reopening the adodataset
  adodataset.close; 
  adodataset.open;
  // move to the wanted record
  adodataset.Locate('id', id, []); 
  
Leonard
  • 306
  • 1
  • 8
-1

Why not use TAdoQuery ?

   Qry.Close;
   Qry.SQL.Clear;
   Qry.SQL.Add("select * from Table1 left join Table2 on Table1.ID = Table2.ID");
   Qry.Open;
   while not Qry.EOF do
   begin
      [do stuff]
      Qry.Next;
   end;
Andrei Pop
  • 81
  • 1
  • 4