请问各位高手:如何在SQL 2005中存储图片?

解决方案 »

  1.   

    一般是存路径.如果要存图片,得使用ntext、text 和 image
    用于存储大型非 Unicode 字符、Unicode 字符及二进制数据的固定长度和可变长度数据类型。Unicode 数据使用 UNICODE UCS-2 字符集。ntext可变长度 Unicode 数据的最大长度为 230 - 1 (1,073,741,823) 个字符。存储大小是所输入字符个数的两倍(以字节为单位)。ntext 在 SQL-92 中的同义词是 national text。text服务器代码页中的可变长度非 Unicode 数据的最大长度为 231-1 (2,147,483,647) 个字符。当服务器代码页使用双字节字符时,存储量仍是 2,147,483,647 字节。存储大小可能小于 2,147,483,647 字节(取决于字符串)。image可变长度二进制数据介于 0 与 231-1 (2,147,483,647) 字节之间。 注释
    下面的函数和语句可以与 ntext、text 或 image 数据一起使用。函数 语句 
    DATALENGTH  READTEXT 
    PATINDEX SET TEXTSIZE 
    SUBSTRING UPDATETEXT 
    TEXTPTR WRITETEXT 
    TEXTVALID 
      

  2.   


    建議存儲圖片路徑就好了,如果存圖片數據類型最好選用IMAGE,SQL存储图片 收藏 
    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
    go3、读入
    sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2go4、读出成文件
    sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
    go如果报textcopy不是可执行文件的话,你就到
    C:\Program Files\Microsoft SQL Server\MSSQL\Binn
    目录下拷备 textcopy.exe到:
    C:\Windows\system32
      

  3.   

    需要用软件把图片转成字节,然后写入SQL
      

  4.   

    看需求,存在OS上调用路径或直接将图片存在DB里都可,各有优势
      

  5.   

    转成二进制C# 代码一、将图片写入数据库中的方法:
     SqlConnection conn=new SqlConnection("server=.;database=pubs;trusted_connection=Yes");
     conn.Open();
     SqlCommand scmd = null;
     string Path = Application.StartupPath + "//Imgren"; //为获取文件的根目录
     int j = 0;
     FileStream fs=null;
     for (int i = 1; i <= 13; i++)  //利用循环添加一次添加图片
     {
         string sql = "insert into tb_Image values(@a,@b)"; //利用参数实现图片添加
         scmd = new SqlCommand(sql, scon);     scmd.Parameters.Add("@a", SqlDbType.Int);
         scmd.Parameters["@a"].Value = i;   //记住该方法,将值存入参数内     byte[] bt = new byte[10240];       //初始化图片大小
         //创建图片写入流
         fs = new FileStream(Path + "//" + i + ".bmp", FileMode.OpenOrCreate, FileAccess.Read);
         fs.Read(bt, 0, bt.Length);  //读取图片的字节数
         scmd.Parameters.Add("@b", SqlDbType.Image, (int)fs.Length);
         scmd.Parameters["@b"].Value = bt; //将图片的字节数存入参数内
         j = scmd.ExecuteNonQuery();
      }
     if (j > 0)
         MessageBox.Show("将图片写入数据库成功!!!", "友好提示");
     else
         MessageBox.Show("将图片写入数据库失败!!!", "友好提示");
     fs.Close(); //关闭释放流资源二、从数据库中读取图片到picturebox中
        根据编号显示图片哦:
        SqlConnection conn=new SqlConnection("server=.;database=pubs;trusted_connection=Yes");
        conn.Open();
        SqlCommand scmd = null;
        string sql = "select I_image from tb_image where I_id=" +int.Parse(textBox1.Text.Trim()) + "";
        scmd = new SqlCommand(sql, scon);
        SqlDataReader red = scmd.ExecuteReader();
        if (red.Read())
        {
          //创建支持存储区的内存流
           MemoryStream ms = new MemoryStream((byte[])red[0]);
           Image img = Image.FromStream(ms, true); //该方法: FromStream()为验证图像的流
           this.pictureBox1.Image = img;
        }
        red.Close(); //关闭资源