SQL2000图片如何存储及读取

VB.NET code
  
  
  1.  Dim   con   As   New   SqlConnection   _   
  2.                 ("Server=YourServer;uid=<username>;pwd=<strong   解password>;database=northwind")   
  3.               Dim   da   As   New   SqlDataAdapter   _   
  4.                 ("Select   *   From   MyImages",   con)   
  5.               Dim   MyCB   As   SqlCommandBuilder   =   New   SqlCommandBuilder(da)   
  6.               Dim   ds   As   New   DataSet()   
  7.     
  8.               da.MissingSchemaAction   =   MissingSchemaAction.AddWithKey   
  9.     
  10.               Dim   fs   As   New   FileStream   _   
  11.                 ("C:/winnt/Gone   Fishing.BMP",   FileMode.OpenOrCreate,   _   
  12.                   FileAccess.Read)   
  13.               Dim   MyData(fs.Length)   As   Byte   
  14.               fs.Read(MyData,   0,   fs.Length)   
  15.               fs.Close()   
  16.               con.Open()   
  17.               da.Fill(ds,   "MyImages")   
  18.               Dim   myRow   As   DataRow   
  19.               myRow   =   ds.Tables("MyImages").NewRow()   
  20.     
  21.               myRow("Description")   =   "This   would   be   description   text"   
  22.               myRow("imgField")   =   MyData   
  23.               ds.Tables("MyImages").Rows.Add(myRow)   
  24.               da.Update(ds,   "MyImages")   
  25.     
  26.               fs   =   Nothing   
  27.               MyCB   =   Nothing   
  28.               ds   =   Nothing   
  29.               da   =   Nothing   
  30.     
  31.               con.Close()   
  32.               con   =   Nothing   
  33.               MsgBox   ("Image   saved   to   database")   
  34.     
  35.   Double-click   Button2,   and   then   add   the   following   code   to   the   Button2_Click   event   handler:   
  36.   Note   You   must   change   uid   <username>   and   pwd   =<strong   password>   to   the   correct   values   before   you   run   this   code.   Make   sure   that   User   ID   has   the   appropriate   permissions   to   perform   this   operation   on   the   database.   
  37.               Dim   con   As   New   SqlConnection   _   
  38.                 ("Server=YourServer;uid=<username>;pwd=<strong   password>;database=northwind")   
  39.               Dim   da   As   New   SqlDataAdapter   _   
  40.                 ("Select   *   From   MyImages",   con)   
  41.               Dim   MyCB   As   SqlCommandBuilder   =   New   SqlCommandBuilder(da)   
  42.               Dim   ds   As   New   DataSet()   
  43.     
  44.               con.Open()   
  45.               da.Fill(ds,   "MyImages")   
  46.               Dim   myRow   As   DataRow   
  47.               myRow   =   ds.Tables("MyImages").Rows(0)   
  48.     
  49.               Dim   MyData()   As   Byte   
  50.               MyData   =   myRow("imgField")   
  51.               Dim   K   As   Long   
  52.               K   =   UBound(MyData)   
  53.     
  54.               Dim   fs   As   New   FileStream   _   
  55.                 ("C:/winnt/Gone   Fishing2.BMP",   FileMode.OpenOrCreate,   _   
  56.                   FileAccess.Write)   
  57.               fs.Write(MyData,   0,   K)   
  58.               fs.Close()   
  59.     
  60.               fs   =   Nothing   
  61.               MyCB   =   Nothing   
  62.               ds   =   Nothing   
  63.               da   =   Nothing   
  64.     
  65.               con.Close()   
  66.               con   =   Nothing   
  67.               MsgBox   ("Image   retrieved")   
SQL COde 
  1. CREATE PROCEDURE sp_textcopy ( 
  2.   @srvname    varchar (30), 
  3.   @login      varchar (30), 
  4.   @password    varchar (30), 
  5.   @dbname      varchar (30), 
  6.   @tbname      varchar (30), 
  7.   @colname    varchar (30), 
  8.   @filename    varchar (30), 
  9.   @whereclause varchar (40), 
  10.   @direction  char(1)) 
  11. AS 
  12. DECLARE @exec_str varchar (255
  13. SELECT @exec_str = 
  14.         'textcopy /S ' + @srvname + 
  15.         ' /U ' + @login + 
  16.         ' /P ' + @password + 
  17.         ' /D ' + @dbname + 
  18.         ' /T ' + @tbname + 
  19.         ' /C ' + @colname + 
  20.         ' /W "' + @whereclause + 
  21.         '" /F ' + @filename + 
  22.         ' /' + @direction 
  23. EXEC master..xp_cmdshell @exec_str  
  24. 2、建表和初始化数据
  25. create table 表名 (编号 int,image列名 image)
  26. go
  27. insert 表名 values(1,0x)    -- 必须的,且不是null
  28. insert 表名 values(2,0x)    -- 必须的,且不是null
  29. go
  30. 3、读入
  31. sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/图片.bmp','where 编号=1','I' --注意条件是 编号=1
  32. sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/bb.doc','where 编号=2','I' --注意条件是 编号=2
  33. go
  34. 4、读出成文件
  35. sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/图片.bmp','where 编号=1','O' --注意条件是 编号=1
  36. sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/bb.doc','where 编号=2','O' --注意条件是 编号=2
  37. go
  38. ************如果报textcopy不是可执行文件的话,你就到
  39. C:/Program Files/Microsoft SQL Server/MSSQL/Binn
  40. 目录下拷备 textcopy.exe到:
  41. C:/Program Files/Microsoft SQL Server/80/Tools/Binn
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值