How to insert a record with an image field to SQL Server
I believe everyone will do this by inserting records into SQL Server. However, if the table contains an image field, it is difficult to solve this problem. Here is an example, which is very secure, at the same time, we will also show you how to dynamically generate controls (for the experts, I have made a wrong shift)
Procedure button1click (Sender: tobject );
VaR
Query: tquery;
Stringstream: tstringstream;
Begin
Stringstream: = tstringstream. Create ('');
Memo. lines. savetostream (stringstream); // create a sequence stream
Stringstream. Position: = 0;
Query: = tquery. Create (Self); // create a temporary Query
Query. databasename: = database. databasename;
With query. SQL do
Begin
Clear;
Append ('insert into test ');
Append ('(ID, content)'); // content is an image field
Append ('values (: ID,: Content )');
End;
With query do
Begin
Parambyname ('id'). asinteger: = strtointdef (edtid. Text, 0 );
Parambyname ('content'). asblob: = stringstream. datastring; // assign a value here
End;
Stringstream. Free; // get rid of it immediately after it is used up
Database. starttransaction; // start transaction control
Try
Query. execsql; // start to insert records
Database. Commit; // submit the transaction
Except
Query. Free; // if an error occurs, the query is free first.
Database. rollback; // roll back the transaction
Raise; // raise an exception again to notify the user
End;
Query. Free;
End;