Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Inserting binary data into SQL table using SQLEXEC()
Message
From
25/10/2004 10:18:09
 
 
To
22/10/2004 17:58:19
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00953897
Message ID:
00954253
Views:
24
>Can anybody offer me any code example(s) for inserting a binary (in my case a .GIF file) into an SQL table using VFP SQLEXEC()?
>
>Thanks in advance!
>
>Andrew

Copy this int a PRG and Run ( VFP8 required )
CLEAR
CLOSE TABLES ALL
SET NULL ON
SET COLLATE TO 'MACHINE'

H=SQLCONNECT() && Use a ODBC Connection here

? SQLEXEC(H,[USE tempdb CREATE TABLE #images (imageId INT IDENTITY,ImageData Image NULL)])

imagefile = HOME(4)+'GIFS\MORPHFOX.GIF'

* FOR PUT DATA INTO A SQL IMAGE, THE TASK IT IS NOT COMPLEX, EXCEPT FOR A OVERHEAD ( CONVERSION AND 2X SIZE TRANSFER )

? SQLEXEC(m.h,[INSERT #images (ImageData) VALUES (0x]+STRCONV(FILETOSTR(m.imagefile),15)+[)])

* the SQLEXEC it is not usable for a direct SELECT ( you have to transform a GENERAL field into a file
* With  a RV or a CA this task it is more direct
* With a CA you can do all the process: read/write/delete

WITH CREATEOBJECT("MSSQLImages")
	.BufferModeOverride = 5 && here exists a bug
	.DataSource = m.h
	.KeyFieldList = "imageId"
	.Tables = "#images"
	.Alias = "images"
	.CursorSchema = "Imageid I NULL DEFAULT 0, ImageData M NOCPTRANS NULL"	&& here exists a Bug
	.SelectCmd = [SELECT * FROM #images]
	.CursorFill
	? "CHECK IF RETURN THE SAME GIF DATA : ",images.ImageData==FILETOSTR(m.imagefile)
	* SHOW THE MEMO IMAGE
	TempImage = ADDBS(SYS(2023))+SYS(2015)+'.gif'
	=ShowTheImage()
	* UPDATE THE IMAGE
	APPEND MEMO ImageData FROM HOME(2)+'WebServices\Northwind\readme1.gif' OVERWRITE
	=TABLEUPDATE()
	* REQUERY
	.CursorRefresh
	* SHOW
	SCAN
		=ShowTheImage()
	ENDSCAN
	* INSERT TWO IMAGES
	APPEND BLANK
	APPEND MEMO ImageData FROM HOME(2)+'Data\Graphics\davonanc.gif' OVERWRITE
	APPEND BLANK
	APPEND MEMO ImageData FROM HOME(2)+'Data\Graphics\smittim.gif' OVERWRITE

	=TABLEUPDATE(.T.)
	? .IdentityList
	* REQUERY
	.CursorRefresh
	* SHOW
	SCAN
		=ShowTheImage()
	ENDSCAN
	.CursorDetach
ENDWITH
? SQLDISCONNECT(M.H)

* INSERT A NEW IMAGE 

DEFINE CLASS MSSQLImages AS CursorAdapter
	BreakOnError   = .T.
	DataSourceType = "ODBC"
	BatchUpdateCount = 1	&& ONLY 1 IT IS SUPPORTED HERE, A SOLUTION WITH BATCHCOUNT>1 IT IS SIMPLE TO FOUND
	HIDDEN BatchUpdateCount
	
	IdentityList	= ""
	
	PROCEDURE CursorFill
	LPARAMETERS lUseCursorSchema , lNoData , nOptions , Source
		NODEFAULT
		RETURN CursorAdapter::CursorFill(.T.,@m.lNoData , @m.nOptions , @m.Source)
	ENDPROC

	PROCEDURE BeforeCursorUpdate
	LPARAMETERS nRows, lForce
		this.IdentityList	= ""
	ENDPROC

	PROCEDURE BeforeUpdate
	LPARAMETERS cFldState, lForce, nUpdateType, cUpdateInsertCmd, cDeleteCmd
		cUpdateInsertCmd = [UPDATE #images SET ImageData=0x]+STRCONV(ImageData,15)+[ WHERE Imageid=]+LTRIM(STR(Imageid))
	ENDPROC
	
	PROCEDURE BeforeInsert
	LPARAMETERS cFldState, lForce, cInsertCmd
		IF SQLEXEC(m.this.DataSource,[INSERT #images (ImageData) VALUES (0x]+STRCONV(ImageData,15)+[) SELECT SCOPE_IDENTITY() insertId ])>0
			This.IdentityList	= m.This.IdentityList + LTRIM(STR(insertId))+","
			USE
			SELECT (m.This.Alias)
		ENDIF
		NODEFAULT
		cInsertCmd = []
	ENDPROC
	
ENDDEFINE

PROCEDURE ShowTheImage
	IF 		STRTOFILE(images.ImageData,m.TempImage)>0
		IF _SCREEN.AddObject("ImgTest","image")
			WITH _SCREEN.ImgTest
				.Picture = m.TempImage
				.vISIBLE = .T.
				WAIT WINDOWS "CLICK"
				.vISIBLE = .F.
				.Picture = ''	
			ENDWITH			
			_SCREEN.RemoveObject("ImgTest")
		ENDIF
	ENDIF
	ERASE (m.TempImage)
ENDPROC
This is the first time that i use a CA,
then this can to be make better.
Previous
Reply
Map
View

Click here to load this message in the networking platform