Paste data into database [Delphi]

I am trying to use the TAdoTable component,

  • In the Create form, I call .Append () and the button I call .Post ()

but it loads the whole table! I do not need to load anything, just need to insert a row into this table.

I was wondering if there is a โ€œgood wayโ€ to insert data into the database with Ado, I have already tried using the โ€œmanualโ€ approach with TAdoCommand, but this does not seem right to me.

  • I generate an INSERT request using Format (), and all string fields are escaped using QuotedStr ()

Thanks in advance!

Arthur.

+4
source share
4 answers

You can use TADODataset (or TADOQuery).

The way I do this sometimes is that a CommandText returns 0 records from a table, i.e. SELECT TOP 0 * FROM [table] , then use .Append and .Post

But personally, I prefer to write SQL, for example, using TADOCommand

+6
source

Use the TADOQuery object if you do not need to display table data.

Basically:

  • Use TADOQuery.SQL.Text to install the SQL command
  • Use the TADOQuery.ExecSQL Method to Run the SQL Command
+6
source

You can also use the TADOCommand component and execute a specific SQL command. If you repeat the same command over and over (for example, inserts into a table), consider using parameters instead of directly modifying SQL for each call. Parameters are easy to use, just put: PARAMNAME in your sql, then use the parameter object on the ado component that you use to set the value. For instance:

Assuming the CommandText of the TAdoCommand component contains " INSERT INTO TABLENAME (FIELD1) VALUES (:FIELDVALUE1) "

 AdoCommand1.Parameters.ParamByName('FIELDVALUE1').Value := 'TEST' AdoCommand1.Execute; 

When the above sql is executed, the string "TEST" will be written to FIELD1.

+2
source
 var CountVar: Integer; begin TADOConnection1.Execute(ASQLInsertStatement, CountVar, [adExecuteNoRecords]); end; 
+1
source

Source: https://habr.com/ru/post/1302529/


All Articles