Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to add document in SQL using SQL pass through ?
Message
De
19/06/2007 18:29:29
 
 
À
19/06/2007 09:57:35
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Database:
MS SQL Server
Divers
Thread ID:
01234226
Message ID:
01234405
Vues:
20
>Yes I know how to do in combination with vfp table or remote, but my question is most with sql passthrough or, in other words, in sql analyzer?
>Is exist a function LOADUP() or something like that to insert a file directly into a sql table?

Marc -

I use the following and it works quite well. I use this to read a file - in my case a JPG - from a SQL Table
PROCEDURE SqlFileImageRead()

LOCAL lcTempFile
LOCAL lnHandle,lnResult
LOCAL lcFileName

lcTempFile = thisapp.oDataHandler.MakeTempFileName() + ".dbf"
THIS.EraseFile(lcTempFile)

lcFileName = thisapp.oDataHandler.scViewPath + ALLTRIM(thisapp.GetNetWorkName())  + this.scFileSuffix + this.scFileExt && ".jpg"
IF FILE(lcFileName)
	ERASE (lcFileName)
ENDIF


* take data from SQL server
lcSQL = [SELECT picture as FileImage FROM MemberPhotos WHERE cMemberID = ?tcMemberID AND cSiteID = ?tcSiteID]
lnResult = SQLExec(this.snHandle,lcSql,"csrFileImage")

IF lnResult <> SQL_ERROR
	* We have a general field in 'csrPhoto' cursor because image type on SQL Server
	* mapped to general field in VFP.

	* save data to temporary table
	SELECT csrFileImage
	COPY TO (lcTempFile)
	USE

	* change general field type to memo
	lnHandle = FOPEN(lcTempFile,12)
	FSEEK(lnHandle,43)
	FWRITE(lnHandle,'M')
	FCLOSE(lnHandle)

	* save data from memo field into file
	USE (lcTempFile) ALIAS tempFileImage
	STRTOFILE(tempFileImage.FileImage,lcFileName)

	USE IN tempFileImage
	THIS.EraseFile(lcTempFile)
ENDIF

RETURN lcFileName
The following will store a file to a SQL Table
PROCEDURE SqlFileImageWrite()
LOCAL lcTempFile,lnHandle,lnResult

lcTempFile = thisapp.oDataHandler.MakeTempFileName() + ".dbf"
THIS.EraseFile(lcTempFile)

* create temporary buffer table
SELECT 0
CREATE TABLE (lcTempFile) FREE (PICTURE M)

* Place binary data from file into memo
APPEND BLANK
APPEND MEMO PICTURE FROM (tcFileName) OVERWRITE
USE

* change memo field type to general to match to image type on SQL Server
lnHandle = FOPEN(lcTempFile,12)
FSEEK(lnHandle,43)
FWRITE(lnHandle,'G')
FCLOSE(lnHandle)

lcEditUser = thisapp.oSecurityHandler.GetUserName()
ltEditTime = DATETIME()

* write data to SQL Server
USE (lcTempFile) ALIAS csrFile

* Check if a record already exists for this file
lcSqlFind = [SELECT cMemberID FROM MemberPhotos WHERE cMemberID = ?tcMemberID AND cSiteID = ?tcSiteID]
lnResult = SQLEXEC(this.snHandle,lcSqlFind,"csrFileExists")
IF lnResult <> SQL_ERROR 
	IF RECCOUNT("csrFileExists") = 0
		lcSql = [insert into MemberPhotos VALUES ] +;
			[(?tcMemberID,?tcSiteID,?csrFile.Picture,?lcEditUser,?ltEditTime,?lcEditUser,?ltEditTime)]

	ELSE
		lcSql = [UPDATE MemberPhotos SET Picture = ?csrFile.Picture, ]+;
			[cUpdateUser = ?lcEditUser,tUpdateDate = ?ltEditTime ] +;
			[WHERE cMemberID = ?tcMemberID AND cSiteID = ?tcSiteID]

	ENDIF
	lnResult = SQLExec(this.snHandle,m.lcSQL)
	ASSERT lnResult <> SQL_ERROR

	USE IN csrFileExists
ENDIF

USE IN csrFile

RETURN lnResult <> SQL_ERROR
Hope that helps!
Carsten M. Thode
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform