http://community.csdn.net/Expert/topic/3472/3472433.xml?temp=.3907282

解决方案 »

  1.   

    主要代码:
    publice string constr;//constr为数据库连接字符串
    private string sqlstr;//sql字符串
    private openFileDialog ofd;
    private byte[] b;
    private Bitmap image;
    private pictureBox pictureBox1;ofd=new openFileDialog();
    FileStream fs=new FileStream(ofd.FileName,FileMode.Open,FileAccess.Read);
    image=new Bitmap(fs);
    pictureBox1.Image=(Image)image;
    b=new byte[fs.Length];
    fs.Read(b,0,b.Length);
    try{
    SqlConnection conn=new SqlConnection(constr);
    conn.open();
    sqlstr=@"insert into table(photo) values(@photo)";
    SqlCommand comm=new SqlCommand(conn,sqlstr);
    comm.Parameters.Add("@photo",SqlDbType.Image,"photo");
    comm.Parameters["@photo"].Value=b
    }
    catch(Exception)
    {
    comm.ExecteNonQuery();
    }
    conn.Close();
      

  2.   

    上面的有几处是错的,修改如下:(c#.net)
    publice string constr;//constr为数据库连接字符串
    private string sqlstr;//sql字符串
    private openFileDialog openFileDialog1;
    private byte[] b;
    private pictureBox pictureBox1;openFileDialog1=new openFileDialog();
    pictureBox1.Image=Image.FileFrom(openFileDialog1.FileName);FileStream fs=new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
    b=new byte[fs.Length];
    fs.Read(b,0,b.Length);
    SqlConnection conn=new SqlConnection(constr);
    conn.open();sqlstr=@"insert into table(photo) values(@photo)";
    SqlCommand comm=new SqlCommand(sqlstr,conn);
    comm.Parameters.Add("@photo",SqlDbType.Image,"photo");
    comm.Parameters["@photo"].Value=b
    try
    {
    comm.ExecteNonQuery();
    }
    catch(Exception err)
    {
    MessageBox.Show(err.Message)
    }
    conn.Close();
      

  3.   

    用外部語言實現比較簡單
    或參照下面大象得
    用image类型保存以上格式文件,写入数据库可以使用writetext()函数:下例将文本指针放到局部变量 @ptrval 中,然后 WRITETEXT 将新的文本字符串放到 @ptrval 所指向的行中。USE pubs
    GO
    EXEC sp_dboption 'pubs', 'select into/bulkcopy', 'true'
    GO
    DECLARE @ptrval binary(16)
    SELECT @ptrval = TEXTPTR(pr_info) 
    FROM pub_info pr, publishers p
    WHERE p.pub_id = pr.pub_id 
       AND p.pub_name = 'New Moon Books'
    WRITETEXT pub_info.pr_info @ptrval 'New Moon Books (NMB) has just released another top ten publication. With the latest publication this makes NMB the hottest new publisher of the year!'
    GO
    EXEC sp_dboption 'pubs', 'select into/bulkcopy', 'false'
    GO------------------------馬可寫的用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
    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:\Program Files\Microsoft SQL Server\80\Tools\Binn