I have to do this:
example.txt
example.txt
save to .zip
with password .zip
file convert to blob and stored into table Do you know how to do it only in T-SQL? Is it possible?
DECLARE @HTML varchar(200)
SET @HTML = '<html>
<META http-equiv=Content-Language content=pl>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-2">
<body style="color:black; font-family: verdana, arial, helvetica, sans-serif; font-size:11px;">
TEST</body></html>'
SELECT @HTML
Using something like this?
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type', 1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @IMG_PATH
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @TIMESTAMP, 2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken
OK I created this:
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
namespace SaveBlobData
{
class SaveHTML
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void SaveBlob(out SqlInt32 value, int idZak)
{
value = 3 + idZak;
}
}
}
I add .dll
but I get error when I select this. Where is problem?
For this part you can use OLE Automation Object
DECLARE @FSO int,
@oFile int,
@filename nvarchar(max) = 'c:\Folder\example.txt'
-- Create OLE Automation Object
EXEC sp_OACreate 'Scripting.FileSystemObject', @FSO OUT
-- Create file
EXEC sp_OAMethod @FSO, 'CreateTextFile', @oFile OUT, @filename, 8 , True
-- Write data
EXEC sp_OAMethod @oFile, 'Write', NULL, @HTML
-- Clear used objects
EXEC sp_OADestroy @FSO
EXEC sp_OADestroy @oFile
Don't know how this could be done with t-sql, for that purpose we use powershell (or cmd):
# path to 7-zip
$zip = "C:\Program Files\7-Zip\7z.exe"
# file
$FilesArh = "c:\Folder\example.zip"
$Dir = "c:\Folder\example.txt"
&$zip a "$FilesArh" "$Dir"
Create file Test1.ps1
with this inside:
param
(
[String] $FilesArh,
[String] $Dir
)
# path to 7-zip
$zip = "C:\Program Files\7-Zip\7z.exe"
&$zip a "$FilesArh" "$Dir"
Launch it from SQL Server:
EXEC xp_cmdshell 'powershell C:\Folder\Test.ps1 "c:\Folder\example.zip" "c:\Folder\example.txt"'
With OPENROWSET you can get BLOB data and INSERT it into table.
INSERT INTO your_table (BLOB_field)
SELECT *
FROM OPENROWSET(BULK 'c:\Folder\example.zip', SINGLE_BLOB) AS BLOB
You can achive this by using CLR Stored Procedure that allows you to access resources being out of SQL Server control. You can write this procedure in .NET (you can do anything you want in this procedure as for example read file, compress it, etc). Then you have to prepare a DLL assembly and connect it with SQL Server. Here you are an example from MSDN how to create a procedure from external assembly:
CREATE ASSEMBLY MyFirstUdp FROM 'C:\Programming\MyFirstUdp.dll';
CREATE PROCEDURE HelloWorld
AS EXTERNAL NAME MyFirstUdp.StoredProcedures.HelloWorld;
EXEC HelloWorld;
More information about CLR Stored Procedures (and presented example) you can find on https://msdn.microsoft.com/pl-pl/library/ms131094(v=sql.110).aspx
Until you have your CLR proceure available in SQL Server then you can call it just from the batch.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.