日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

winxp上传文件到服务器,通过SQLServer的xp_cmdshell在服务器之间传送文件

發布時間:2023/12/10 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 winxp上传文件到服务器,通过SQLServer的xp_cmdshell在服务器之间传送文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

xp_cmdshell作為sql Server的擴展存儲過程之一,也是sql Server在安全大敵,很多sql安全手冊上都要求關閉此過程,這不利用其特性簡要實現了一個在sql服務器之間傳取文件的功能,在sql2005下測試通過,現貼出代碼下,大家共賞之

/*

* 腳本:通過sqlServer的xp_cmdshell在服務器之間傳送文件

通過sql sqlServer實現,要求支持遠程訪問,并且均開啟xp_cmdshell選項

* 作者: qin

* 整理: 2013.07.07 13:20

*

* E-Mail:QinGang@sina.com

* QQ:45958462

*/

/*

--開啟高級選項

EXEC sp_configure 'show Advanced options','1'

RECONFIGURE

EXEC sp_configure 'xp_cmdshell',1

RECONFIGURE WITH OVERRIDE

*/

set nocount on;

--參數

declare @FromInstrance nvarchar(100),@FromDB nvarchar(100),@FromPWD nvarchar(100);

declare @ToInstrance nvarchar(100),@ToDB nvarchar(100),@ToPWD nvarchar(100);

declare @sql nvarchar(max),@cmd nvarchar(4000);

declare @file_table nvarchar(100);

declare @upfile_path varchar(100),@newfile_path varchar(100),@downfile_name varchar(100);

declare @error_flag int;

set nocount on;

--修改參數值

--文件名

set @upfile_path='D:\Data_bak\test.bak';--上傳文件名

set @newfile_path=@upfile_path;--預留

set @downfile_name='D:\Data_bak\test3.bak';--下載后文件名

--源服務器

set @FromInstrance = '192.168.80.1';

set @FromPWD = '123asd';

set @FromDB = 'tempdb';

--目標服務器

set @ToInstrance = '192.168.80.130';

set @ToPWD = '123asd';

set @ToDB = 'tempdb';

--正式執行,不要修改

set @file_table='[tmp_file_'+cast(RAND()*1000000 AS VARCHAR)+']';

print 'select *,datalength(sText) file_len from tempdb.dbo.'+@file_table;

--上傳

--1、生成upload.sql腳本(刪除表+上傳)

set @sql='if object_id(''tempdb..'+@file_table+''') is null

create table tempdb..'+@file_table+'(id int,sText nvarchar(max));

truncate table tempdb..'+@file_table+';

insert into tempdb..'+@file_table+'(id)values(1);

update tempdb..'+@file_table+' set sText=(select BULKColumn FROM OPENROWSET(BULK N'''+@newfile_path+''',SINGLE_BLOB) AS Data) where id=1;';

--print @sql;

--2、在本地釋放文件upload.sql

if object_id('tempdb.dbo.tmp_table') is not null

drop table tempdb.dbo.tmp_table;

select cast(@sql as nvarchar(max))as sText into tempdb.dbo.tmp_table;

exec master..xp_cmdshell 'bcp tempdb.dbo.tmp_table out "c:\upload.sql" -T -t"|" -w -q',no_output;

--select * from tempdb.dbo.tmp_table

--3、連接源端執行upload.sql,文件放在源端tempdb庫

set @cmd='osql -S '+@FromInstrance+' -U sa -P '+@FromPWD+' /d '+@FromDB+' /i c:\upload.sql';

--print (@cmd);

exec master..xp_cmdshell @cmd,no_output;

--下載

--1、創建download.sql腳本

set @sql='exec master..xp_cmdshell ''bcp "select sText from tempdb.dbo.'+@file_table+' where ID = 1" queryout "'+@downfile_name+'" -N -q -S"'+@FromInstrance+'" -U"sa" -P"'+@FromPWD+'" '''

--print @cmd;

--2、在本地釋放文件download.sql

if object_id('tempdb.dbo.tmp_table') is not null

drop table tempdb.dbo.tmp_table;

select cast(@sql as nvarchar(max))as sText into tempdb.dbo.tmp_table;

exec master..xp_cmdshell 'bcp tempdb.dbo.tmp_table out "c:\download.sql" -T -t"|" -w -q',no_output;

--select * from tempdb.dbo.tmp_table

--3、連接源端執行upload.sql,文件放在源端tempdb庫

set @cmd='osql -S '+@ToInstrance+' -U sa -P '+@ToPWD+' /d '+@ToDB+' /i c:\download.sql';

--print (@cmd);

exec master..xp_cmdshell @cmd,no_output;

--恢復現場:刪除源端臨時表

--1、生成upload.sql腳本(刪除表)

set @sql='if object_id(''tempdb..'+@file_table+''') is not null

drop table tempdb..'+@file_table+';';

--print @sql;

--2、在本地釋放文件upload.sql

if object_id('tempdb.dbo.tmp_table') is not null

drop table tempdb.dbo.tmp_table;

select cast(@sql as nvarchar(max))as sText into tempdb.dbo.tmp_table;

exec master..xp_cmdshell 'bcp tempdb.dbo.tmp_table out "c:\upload.sql" -T -t"|" -w -q',no_output;

--select * from tempdb.dbo.tmp_table

--3、連接源端執行upload.sql(執行刪除表)

set @cmd='osql -S '+@FromInstrance+' -U sa -P '+@FromPWD+' /d '+@FromDB+' /i c:\upload.sql';

--print (@cmd);

exec master..xp_cmdshell @cmd,no_output;

--4、刪除本地表

if object_id('tempdb.dbo.tmp_table') is not null

drop table tempdb.dbo.tmp_table;

--5、刪除臨時文件

set @cmd='if exist c:\upload.sql del c:\upload.sql /f /s /q';

exec master..xp_cmdshell @cmd,no_output;

set @cmd='if exist c:\download.sql del c:\download.sql /f /s /q';

exec master..xp_cmdshell @cmd,no_output;

/*

--關閉高級選項

EXEC sp_configure 'xp_cmdshell',0

RECONFIGURE

EXEC sp_configure 'show Advanced options','0'

RECONFIGURE WITH OVERRIDE

*/

總結

以上是生活随笔為你收集整理的winxp上传文件到服务器,通过SQLServer的xp_cmdshell在服务器之间传送文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。