How to insert SQL Server into records with image fields

zhaozj2021-02-16  63

How to insert SQL Server into records with image fields

Inserting records to SQL Server, I believe everyone will do it, but if there is an image field with the image in the table, there is an example, very safe, and also show the skills of dynamically generated controls (for masters to the masters)

Procedure Button1Click (Sender: TOBJECT);

VAR

Query: tQuery;

Stringstream: TSTRINGSTREAM;

Begin

StringStream: = TstringStream.create ('');

Memo.lines.savetostream (Stringstream); // Create a character stream here

Stringstream.position: = 0;

Query: = tQuery.create (Self); // Create a temporary query here

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: = startdef (edtid.text, 0);

PARAMBYNAME ('content'). Asblob: = stringstream.dataString; // is assigned here

END;

Stringstream.free; // After using it, you will fall.

Database.startTransaction; // Start transaction control

Try

Query.execsql; // Start insertion record

Database.commit; // Submit a transaction

Except

Query.Free; // If an error first puts the query free

Database.rollback; // Retreat

Raise; // Re-lead an exception to inform users

END;

Query.Free;

END;

转载请注明原文地址:https://www.9cbs.com/read-26862.html

New Post(0)