views:

459

answers:

4

I'm trying using a TAdoTable component,

  • On form Create I call .Append() and in a button i call .Post()

but it loads the entire table! I don't need it to load anything, just need to insert a row into this table.

I was wondering if there is "good way" of inserting data into database with Ado, i already tried using the a "manual" approach with TAdoCommand but it doesn't seems right to me

  • I generate the INSERT query using Format() and all string fields are escaped with QuotedStr()

Thanks in advance!

Arthur.

+4  A: 

Use the TADOQuery object if you don't need to display the table's data.

Basically:

  • Use TADOQuery.SQL.Text to set the SQL command
  • Use TADOQuery.ExecSQL method to fire the SQL command
AlexV
+4  A: 

You can use a TADODataset (or TADOQuery).

The way I do it sometimes is with by setting the CommandText to return 0 records from the table, i.e. SELECT TOP 0 * FROM [table], then use .Append and .Post

But personally, I prefer writing the SQL, such as with a TADOCommand

jasonpenny
This is what i was looking for. It's not pretty to "manually" mount an insert with 13 fields.
arthurprs
instead of "select top 0" you can also use a where clause like "select * from Table where PrimaryKeyField = 0". Just make sure to use a value that will never exist in the table
Joe Meyer
or use `WHERE 1=2`
Gerry
+2  A: 

You can also use the TADOCommand component, and have it execute the specific SQL command. If you find yourself performing the same command over and over again (like inserts into a table) then consider using parameters rather than directly changing the SQL for every call. Parameters are easy to use, just place a :PARAMNAME in your sql, then use the parameters object on the ado component your using to set the value. For example:

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, then the string "TEST" would be written to FIELD1.

skamradt
+1  A: 

var CountVar: Integer;

begin

  TADOConnection1.Execute(ASQLInsertStatement, CountVar, [adExecuteNoRecords]);

end;
Larry Lustig