想在数据库里存一些图片资源,建表后,把某一列的数据类型定为 varbinary 。本人菜鸟,据说image类型不怎么受推荐了,所以就用varbinary类型了,但是不知道在SSMS中如何初始化图片数据。请问该如何可视化的初始化数据库中的图片数据呢?这个不用非得使用编程的方式吧?如果必须用的话,顺便请高手说说如何用C#处理与数据库图片资源的交互的问题吧~~~
谢谢~~~~

解决方案 »

  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 
    =======
    不过还是建议存路径,相关信息如下:
    建議存儲圖片路徑就好了,如果存圖片數據類型最好選用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
      

  2.   

    以下是一个C#示例:
    转成二进制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(); //关闭资源
      

  3.   

    /// <summary>
            /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
            /// </summary>
            /// <param name="strSQL">SQL语句</param>
            /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
            /// <returns>影响的记录数</returns>
            public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand(strSQL, connection);
                    System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
                    myParameter.Value = fs;
                    cmd.Parameters.Add(myParameter);
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException E)
                    {
                        throw new Exception(E.Message);
                    }
                    finally
                    {
                        cmd.Dispose();
                        connection.Close();
                    }
                }
            }