How to insert a record with an image field to SQL Server

Source: Internet
Author: User
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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.