c# - Can I stream a file to a SQL Server stored procedure? -
i have file on remote machine want 'copy' sql server host machine specific area. i'm wondering options doing through sql server stored procedure, , more want stream file contents, not reading entire contents memory first.
i'm looking streamed version of this:
create procedure [dbo].[savefile] (@filename nvarchar(255), @contents nvarchar(max)) begin declare @objecttoken int exec sp_oacreate 'adodb.stream', @objecttoken output exec sp_oasetproperty @objecttoken, 'type', 1 exec sp_oamethod @objecttoken, 'open' exec sp_oamethod @objecttoken, 'writetext', null, @contents exec sp_oamethod @objecttoken, 'savetofile', null, 'c:\\destination\\' + @filename, 2 exec sp_oamethod @objecttoken, 'close' exec sp_oadestroy @objecttoken end
- can have
filestream
or equivalent stored procedure input parameter? - have clr stored procedure handle stream?
- perhaps 1 procedure open stream , return
@objecttoken
,writetext
chunk stream, , third proceduresavetofile
,close
it, seems!a.goodidea
, i'd have ensure closure , destruction through sort of timeout. - other options?
it's being sourced c# console application , i'd prefer stream these large files instead of file.readalltext()
, loading multi-gigabyte string variable, , calling stored procedure entire contents of file.
i made few attempts working ole automation (sp_oacreate
, sp_oamethod
, etc.) , ran number of issues , made clr-based solution.
i have c# console application, c# clr stored procedures , sample sql server database posted on my github account in repository named streamfile.
the basic idea call procedure initiate transfer logs row in database , creates file, send n
chunks different stored procedure appends file, , call third stored procedure denote successful completion of streaming.
create procedure [dbo].[streamfile_addbytes] (@fileid int, @chunk varbinary(max)) begin begin try declare @tmppath nvarchar(max) select @tmppath = filepath streamedfiles streamedfileid = @fileid if (@tmppath null) raiserror (n'invalid streamedfileid provided: %d', 11, 1, @fileid); --c# clr based stored procedure exec dbo.appendbytes @tmppath, @chunk update streamedfiles set chunks = chunks + 1, filesize = filesize + len(@chunk) streamedfileid = @fileid select * streamedfiles sf sf.streamedfileid = @fileid end try begin catch declare @errormessage nvarchar(4000), @errorseverity int, @errorstate int; select @errormessage = error_message(), @errorseverity = error_severity(), @errorstate = error_state(); raiserror (@errormessage, @errorseverity, @errorstate); end catch
Comments
Post a Comment