在.NET平台下使用SQL2000 Image类型数据

问题描述:

     SQL Server 2000中的ntext、text 和 image数据类型用于存储大型非 Unicode 字符、Unicode 字符及二进制数据的固定长度和可变长度数据类型。其中image可变长度二进制数据介于 0 与 231-1 (2,147,483,647) 字节之间。对于上述三种数据的存取一直是使用SQL Server2000 的一个难点。本实验实现在.NET平台下对SQL Server 2000 Image类型数据的存取操作。

实验目的:

1、熟悉文件的读写操作

2、掌握MemortStream类的使用

3、提高T-SQL脚本的编写能力

4、复习SqlParameter类和Image类

5、提高WinForm编程能力

问题分析:

在SQL Server 2000中使用Image数据类型的方法:
  1、建立过程  
  CREATE   PROCEDURE   sp_textcopy   (    
      @srvname         varchar   (30),    
      @login             varchar   (30),    
      @password         varchar   (30),    
      @dbname             varchar   (30),    
      @tbname             varchar   (30),    
      @colname         varchar   (30),    
      @filename         varchar   (30),    
      @whereclause   varchar   (40),    
      @direction     char(1))    
  AS    
  DECLARE   @exec_str   varchar   (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      
   
  2、建表和初始化数据  
  create   table   表名   (编号   int,image列名   image)  
  go  
  insert   表名   values(1,0x)         --   必须的,且不是null  
  insert   表名   values(2,0x)         --   必须的,且不是null  
  go  
   
  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  

通过对上述代码分析可以知道在SQL查询分析器中使用如下脚本“insert into p(pic)values('C:"Documents and Settings"All Users"Documents"My Pictures"示例图片"Water lilies.jpg')”是可以把图片保存在数据库中的。

2SqlParameter用于表示 SqlCommand 的参数中的参数,通过文件路径传递的是文件的内容,当然,SqlParameter类的Value属性也可以传递文件内容

关键代码解析

1、存储图片

//第一步:建立并打开文件流

              FileStream fs = File.Open(this.openFileDialog1.FileName,FileMode.Open

                  ,FileAccess.Read,FileShare.None);

              //第二步:声明byte型数组

              byte [] b = new byte[fs.Length];

              //第三步:读取文件内容

              fs.Read(b,0,(int)b.Length);

              fs.Close();

              /*

               * 存储一张图片到SQL 2000数据库

               * 操作方式与存储一般数据相类似

               * */

              //第四步:建立、打开数据库连接

              SqlConnection con = new SqlConnection("server =.;database = bdqn;uid = sa;pwd = sa;");

              con.Open();

              //第五步:实例化Command对象

              SqlCommand com = con.CreateCommand();

              com.CommandText="insert into p (pic)values(@pic/*@pic 是存储过程参数*/)";

              //为SqlCommand对象的SqlParameter 对象添加@pic属性

              SqlParameter sp =new SqlParameter("@pic",SqlDbType.Image);

              sp.Direction = ParameterDirection.Input;

              sp.Value = b;

              com.Parameters.Add(sp);

              //执行操作

              com.ExecuteNonQuery();

              con.Close();

2、读取图片

private void button2_Click(object sender, System.EventArgs e)

       {

           SqlConnection con = new SqlConnection("server =.;database = bdqn;uid = sa;pwd = sa;");

           con.Open();

           SqlCommand com = con.CreateCommand();

           com.CommandText="select pic from p where id = 1";

           //强制转换为byte【】数组

           byte []b = (byte[])com.ExecuteScalar();

           con.Close();

           //通过MemoryStream流实例化Image对象

           MemoryStream ms = new MemoryStream(b,0,(int)b.Length);

           this.pictureBox1.Image = Image.FromStream(ms);

       }

存在问题及解决

问题扩充

posted on 2007-10-25 11:49  段静迪  阅读(484)  评论(0编辑  收藏  举报