#Define crlf Chr(13) + Chr(10)

Local lcColoane, lcVals, lcAdresa, lcTabela, lcTable, nr, i, lnSucces, lcSql
External Array lacols
lcColoane = []
lcVals = []
lnSucces = 0

gnconn = SQLConnect([jcsserver],[test_alex],[123])
If gnconn < 0
	Return
Endif

lcAdresa = Addbs(Justpath(Sys(16,0)))
lcTabela = [xitems]
lcTable = lcAdresa + lcTabela
Use (lcTable)
Select 1

nr = Afields(lacols,lcTabela)
For i = 1 To nr
	lcColoane = lcColoane + lacols(i,1) + [,]
	lcVals = lcVals + [?] + lacols(i,1) + [,]
Endfor
lcColoane = Left(lcColoane,Len(Alltrim(lcColoane)) - 1)
lcVals = Left(lcVals,Len(lcVals) - 1)
lcSql = [insert into ] + lcTabela + [(] + lcColoane + [)] + [ values(] + lcVals + [)]

SQLSetprop(gnconn,"Transactions",2)
Scan  
	lnSucces = executaSql(lcSql,'',.T.)
	If lnSucces < 0
		Exit
	Endif
Endscan

If lnSucces > 0
	Sqlcommit(gnconn)
	Messagebox([sa facut importu])
Else
	Sqlrollback(gnconn)
Endif
SQLSetprop(gnconn,"Transactions",1)

SQLDisconnect(gnconn)

**********************************************************
Function executaSql
	Lparameters cmdSql, crsName, tlShowErr
	Local lnSucces

	lnSucces = -1

	If Pcount() < 2 And !m.tlShowErr
		crsName=""
	Endif

	If gnconn > 0
		lnSucces = SQLExec(gnconn,cmdSql,crsName)

		If lnSucces < 0 And m.tlShowErr
			Aerror(laEroare)
			eroaresql(@laEroare,cmdSql)
		Endif
	Endif

	Return lnSucces
Endfunc

**********************************************************
Procedure eroaresql
	Lparameters laErr, lcSql

	External Array laErr
	Local lcMesaj

	lcMesaj = [Eroare # : ] + Alltrim(Str(laErr(1))) + crlf + ;
		[Mesajul : ] + laErr(2) + ;
		IIF(!Empty(laErr(3)), [Eroare OLE : ] + laErr(3) ,[]) + ;
		[Aplicatie : ] + laErr(4) + ;
		IIF(Pcount() = 2, crlf + lcSql, [])
	Messagebox(lcMesaj,0+16)
Endproc
