我用的是sql 2005,现在想在数据库的图形界面做个数据库 其中有一栏是图片的,我试了几次都没成。这该怎么弄啊!!还有在编程时读取图片应该注意哪些方面!  希望有具体的步骤,特别是图片存储方面。谢谢

解决方案 »

  1.   

    我常用的思路是:
    1、数据表中保存图片的字段类型设为Image
    2、把图片读到流
    3、从流中取出为Byte[]
    4、保存时把Byte[]写入到数据表的Image字段中。
    5、取出数据显示时,则与保存相反的顺序。
      

  2.   

    保存二进制数据到数据库
    using(SqlConnection conn = new SqlConnection(""))
    {   
      conn.Open();   
      SqlCommand cmd = new SqlCommand("insert into Tb values(@img)", conn);   
      byte[] b= new byte[60000];   
      FileStream fs = new FileStream("", FileMode.Open, FileAccess.Read);   
      fs.Read(b, 0, 60000);   
      cmd.Parameters.Add("@img", SqlDbType.Image, (int)fs.Length);   
      cmd.Parameters["@img"].Value = b;   
      cmd.ExecuteNonQuery();   
      conn.Close();   
      } 
    读取
    using(SqlConnection conn=new SqlConnection(""))  
    {  
    string strSql="select img from Tb";  
    SqlCommand cmd=new SqlCommand(strSql,conn);  
    conn.Open();  
    SqlDataReader reader=cmd.ExecuteReader();  
    if(reader.Read())  
    {  
    MemoryStream ms=new MemoryStream((byte[])reader["img "]);  
    Image image=Image.FromStream(ms,true);  
    picturebox1.Image=image;  
    }  
    reader.Close();  
    conn.Close();  
    }  
      

  3.   

    我一般都是存储路径的,然后读取的时候用 this.images.ImageUrl = "images/" + ds.Tables[0].Rows[0]["路径"].ToString()
      

  4.   

    SqlDbType.Image <=> byte[] <=> Stream