如何在SQLServer2005中直接存储图片,像是字符串一样存储,还是怎样?急需解决!

解决方案 »

  1.   

    具体怎么存,偶也么搞过。
    不过可以参考下这里1. 写操作(WRITETEXT)
    这里一般要用到的函数有TextPtr获得文本字段的指针,和TextVaild检验指针的有效性,@@RowCount判断返回记录的条数。
    其基本方法是:用Textptr函数得到指针,判断其有效性,用Writetext写数据
    函数说明:Textptr(字段名)。Writetext tablename。Fieldname @textptr(指针) [With Log] data(数据)
    例如:
    Begin Tran
    Declare @Mytextptr VarBinary(16)
    Select @mytextptr=textptr(pr_info)
    From Pub_Info (updlock)
    Where pud_id=’9999’
    IF @Mytextptr Is Not Null
    Writetext pub_info.pr_info @mytextptr with log ‘data’
    Commit Tran
    2. 读操作
    常用函数
    PatIndex(‘%exp%’,var|fieldname。。)
    Datalength()
    @@TextSize 文本大小
    SettextSize N 设置文本大小
    ReadText {TableName。FieldName} {@textptr} Offet Size [HoldLock]
    例如:
    begin tran
    Declare @mytextptr Varbinary(16),@Totalsize int,@Readsize int,@lastread int
    Set textsize 100
    Select @mytextptr=textptr(pr_info), @totalsize=datalength(pr_info)
    @lastread=0,
    @readsize= case when (textsize
    eles datalength(pr_info)
    end
    From Pub_info
    Where Pub_id=’1622’
    IF @mytextptr Is not Null and @readsize>0
    While (@lastread<@totalsize)
    ReadText pub_info.pr_info @mytextptr @lastread @readsize holdlock
    If (@@error<>0)
    Break
    Select @lastread=@lastread+@readsize
    If ((@readsize+@lastread)>@totalsize)
    Select @readsize=@totalsize-@lastread
    End
    Commit Tran
    3.数据更新UpdateText
    更新数据代替了写操作,其基本语法是:
    UpdateText Table_Name.Col_Name Text_Ptr Offest(偏移量) Deleted_Length
    [With Log] [Inserted_Data|Table_Name.Scr_Column_name Str_Text_Ptr]
    说明:
    Offest:0说明从开头开始,Null表示你向当前内容追加数据。
    Deleted_Length:0表示不删除任何内容,Null表示删除所有内容。
    例如1(完全代替):
    Declare @mytextptr varbinary(16)
    Begin tran
    Select @mytextptr=textptr(pr_infro) from pub_info(uplock) where pub_id=’9999’
    If @mytextptr is not null
    Updatetext pub_info.pr_infro @mytextptr 0 null with log “you are right”
    Commit
    例如2:
    declare @mytextptr varbinary(16) ,@offest int
    Begin tran
    Select @mytextptr=textptr(pr_infro),@offest=patindex(‘%D.C%’,pr_infro)-1+4
    /*减一是因为有一个矫正的偏移量,加4是因为D.C.是4*/
    from pub_info(unlock) where pub_id=’0877’
    If @mytextptr is not null and @offest>=0
      

  2.   

    2005可以直接使用openrowset(bulk)导入图片.SSIS中的数据流任务中的导入列,导出列,可以导入,导出图片.
      

  3.   

    CREATE TABLE tb(id int,name varchar(20),photo varbinary(MAX))  --2005不推荐使用image改为varbinary(MAX)INSERT tb(id,name,photo)
        SELECT 1,'lan',img
        FROM OPENROWSET(BULK 'c:\me.jpg',SINGLE_BLOB) AS T(img);
      

  4.   

    使用image类型,存储的是二进制,干嘛还要相对路径?
    把图片保存进数据库时,直接将图片转换为二进制,保存进数据库,
    同样读取时也是直接将图片字段读取为二进制,再转换成图片
      

  5.   


    //保存:
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(openFileDialog1.ShowDialog()==DialogResult.OK)
    {
    string str=openFileDialog1.FileName;
    FileStream fs=new FileStream(str,FileMode.Open,FileAccess.Read);
    Byte[] bt=new byte[fs.Length];
    fs.Read(bt,0,bt.Length);
    fs.Close(); SqlConnection conn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
    conn.Open();
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText="insert into photo values (@image)";
    cmd.Connection=conn;
    SqlParameter para=new SqlParameter("@image",SqlDbType.Image);
    para.Value=bt;
    cmd.Parameters.Add(para);
    cmd.ExecuteNonQuery();
    conn.Close();
    }
    } private void button2_Click(object sender, System.EventArgs e)
    {
    SqlConnection conn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
    conn.Open();
    SqlCommand cmd=new SqlCommand();
    cmd.Connection=conn;
    cmd.CommandText="select image from photo where id=4";
    Byte[] by=(byte[])cmd.ExecuteScalar(); if(saveFileDialog1.ShowDialog()==DialogResult.OK)
    {
    string str=saveFileDialog1.FileName;
    FileStream fs=new FileStream(str,FileMode.Create,FileAccess.Write);
    fs.Write(by,0,by.Length);
    fs.Flush();
    fs.Close();
    } }
      

  6.   

    如果我是用varchar类型呢?那就需要写相对路径了,这样可以减少数据库的大小,提高访问速度吧
    现在我用的是保存相对路径,就是不知道如何写相对路径以及怎么把这个相对路径里的图片绑定到picturebox上,然后每选择一个用户就会有相应的图像显示在窗体上
      

  7.   


    用相对路径的话更简单了。把图片路径保存到数据库里去,然后在显示图片的时候,先把图片路径读出来,更根据路径获取图片对象。
    使用:pictureBox1.Image = System.Drawing.Image.FromFile(图片路径);
    去设置就行了
      

  8.   

    提醒一下,image类型将在未来的版本中删除,2005或以后版本推荐使用varbinary(MAX)
      

  9.   

    谢谢!我先尝试一下,因为我对这个还不是很了解,需要不断学习!
    那么,那个图像字段里面的图片是不是该保存在我的项目里?
    例如,在我的WindowsForm项目新建一个文件夹保存图片,然后到数据库图像字段下写相对路径,再者,到代码中写读取路径,最后再在窗体上的pictureBox显示出来。