使用textcopy对sqlserver的Image字段进行读取和写入

由于类型的特殊性,对于sqlserver 2000的image字段的读取和写入操作通常会给人带来一些麻烦。应急的时候,我们并不想去重新写代码来操作数据。因此大多数情况下人们会使用textcopy这个工具(老早老早以前有人说可以用存储过程,实际上存储过程中最终也是调用的textcopy)。由于没有gui(提示符下),一些初学这可能会有些困惑。前两天就遇到一个,因此现在在将这个古老的小问题拿出来聊两句,呵呵。
  说实话,也不想多说。运行->cmd->进入提示符下然后cd进入'disc:\Program Files\Microsoft SQL Server\MSSQL\Binn>'目录敲入textcopy,跟着提示一步一步操作就行了。不明白的地方看下边;)

上传交互(cmd)操作如下:
 1 C:\Program Files\Microsoft SQL Server\MSSQL\Binn>textcopy
 2 TEXTCOPY Version 1.0
 3 DB-Library version 8.00.194
 4 Type the SQL Server to connect to:  220.**.**.*      
 5 Type your login:  sa
 6 Type your password:  sa
 7 Type the database:  Product
 8 Type the table:  Image
 9 Type the text or image column:  data
10 Type the where clause:  where id=16
11 Type the file:  c:\abc.gif
12 Type the direction ('I' for in, 'O' for out): I
13 Da ta copied into SQL Server image column from file 'c:\abc.gif'.
下载:
 1 C:\Program Files\Microsoft SQL Server\MSSQL\Binn>textcopy
 2 TEXTCOPY Version 1.0
 3 DB-Library version 8.00.194
 4 Type the SQL Server to connect to:  220.**.**.* 
 5 Type your login:  sa
 6 Type your password:  sa
 7 Type the database:  Product
 8 Type the table:  Image
 9 Type the text or image column:  data
10 Type the where clause:  where id=16
11 Type the file:  c:\abc.gif
12 Type the direction ('I' for in, 'O' for out):  O
13 Da ta copied out of SQL Server image column into file 'c:\abc.gif'..
用存储过程方法:
1
、建立过程CREATEPROCEDURE sp_textcopy (
@srvnamevarchar (30),
@login
varchar (30),
@password
varchar (30),
@dbname
varchar (30),
@tbname
varchar (30),
@colname
varchar (30),
@filename
varchar (30),
@whereclausevarchar (40),
@direction
char(1)
)
AS
Begin
DECLARE@exec_strvarchar (255)
SELECT@exec_str=
'textcopy /S'+@srvname+
' /U'+@login+
'
/P'+@password+
'
/D'+@dbname+
'
/T'+@tbname+
'
/C'+@colname+
' /W "'+@whereclause+
'" /F'+@filename+
' /'+@direction
EXEC
master..xp_cmdshell@exec_str
End
2
、建表和初始化数据
create
table 表名 (编号int,image列名image)goinsert 表名 values(1,0x)-- 必须的,且不是null
insert 表名 values(2,0x)-- 必须的,且不是nullgo
3
、读入sp_textcopy'你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I'--注意条件是 编号=1
sp_textcopy
'你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I'--注意条件是 编号=2
go

4
、读出成文件sp_textcopy'你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O'--注意条件是 编号=1
sp_textcopy
'你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O'--注意条件是 编号=2
go

如果报textcopy不是可执行文件的话,你就到
C:\Program Files\Microsoft SQL Server\MSSQL\Binn
目录下拷备 textcopy.exe到:
C:\Program Files\Microsoft SQL Server\
80\Tools\Binn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值