RT

解决方案 »

  1.   

    string filePath=this.openFileDialog1 .FileName;
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs); byte [] photo = br.ReadBytes((int)fs.Length);
    br.Close();
    fs.Close(); SqlConnection cn = new SqlConnection(db.getIP());
    SqlCommand cmd = new SqlCommand(sql, cn);
    cmd.Parameters.Add("@Picture", SqlDbType.Image, photo.Length).Value = photo;
    cmd.Parameters.Add("@burea", SqlDbType.Char).Value = bb.Text.Trim (); cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
      

  2.   

    //把图片读到pictureBox,再写入数据库SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
    conn.Open();
    SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
    byte[] ib=new Byte[60000];
    FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
    fs.Read(ib,0,60000);
    cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
    cmd.Parameters["@i"].Value=ib;
    cmd.ExecuteNonQuery();
    conn.Close();-------------------------------------------------------
    //从数据库读图片到pictureboxSqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
    conn.Open();
    SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
    SqlDataReader reader=cmd.ExecuteReader();
    reader.Read();
    MemoryStream buf=new MemoryStream((byte[])reader[0]);
    Image image=Image.FromStream(buf,true);
    pictureBox1.Image=image;
      

  3.   

    #region  图片存储到数据库
    public void imgstore(HtmlInputFile UP_FILE,string strsql,string ImageData,string ImageContentType,string ImageSize,Label txtMessage)
    {
    Int32 FileLength = 0;     
    HttpPostedFile UpFile = UP_FILE.PostedFile;  //HttpPostedFile对象,用于读取图象文件属性
    FileLength = UpFile.ContentLength;     //记录文件长度 
    try 
    {
    if (FileLength != 0) 
    {
    Byte[] FileByteArray = new Byte[FileLength];   //图象文件临时储存Byte数组
    objstream= UpFile.InputStream;      //建立数据流对像
    //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
    objstream.Read(FileByteArray,0,FileLength);   

    objcomm = new SqlCommand(strsql,GetCN());
    objcomm.Parameters.Add(ImageData,SqlDbType.Binary, FileLength).Value = FileByteArray;
    objcomm.Parameters.Add(ImageContentType, SqlDbType.VarChar,50).Value = UpFile.ContentType;  //记录文件类型

    //记录文件长度,读取时使用
    objcomm.Parameters.Add(ImageSize, SqlDbType.BigInt,8).Value = UpFile.ContentLength;
    objcomm.ExecuteNonQuery(); 
    CloseCN();
    txtMessage.Text = "<b>OK!你已经成功上传你的图片</b>";//提示上传成功
    }

    catch (Exception ex) 
    {
    txtMessage.Text = ex.Message.ToString();
    }
    }

    #endregion #region  从数据库中提取图片信息以显示
    public void ShowPhoto(string strId,string tblname,string ImageData,string ImageContentType,string ImageSize)
    {
    string strsql = "select "+ImageData+","+ImageContentType+","+ImageSize+" from "+tblname+" where id='"+strId+"'";
    objdr = GetReader(strsql);
    if(objdr.Read() && objdr[ImageSize].ToString()!="")
    {
    HttpContext.Current.Response.ContentType = objdr[ImageContentType].ToString();//设定输出文件类型
    //输出图象文件二进制数制
    HttpContext.Current.Response.BinaryWrite( (byte[]) objdr[ImageData] );

    }
    CloseCN();
    }
    #endregion
      

  4.   

    从数据库读出数据在网页上显示这副图我还是没搞懂我想在ImageButton上显示这副图
      

  5.   

    我是初学者乱说的,说错了不要笑我,好像sql不能存储图片吧,只能存储路径吧。
      

  6.   


    从数据库读出数据在网页上显示这副图我还是没搞懂我想在ImageButton上显示这副图正解在哪里?
      

  7.   

    那我每增加一条记录就要手动把这副图放到网站上去吗?还是有用FileUpload控件把图保存在网站上啊,如果这样的,该怎么做啊
      

  8.   

    我界面上有个浏览按纽,用户选中了某副图后点上传后。图片保存到数据库,页面上能显示出这副图。我想问如果是用2进制的方式读写数据库的话,该怎么从数据库读出来(写我能实现,读有问题)用ImageButton显示出来。如果是保存地址的话,我怎么把这副图保存到网站服务器上去,还有怎么把这副图在网站的路径保存到数据库啊。
      

  9.   

    当然可以存图片
    不过是二进制
    1L,3L ,4L
    写用二进制存
    读用二进制读
      

  10.   

    ================================
    据库结构
    create table test
    {
      id int identity(1,1),
      FImage image

    相关的存储过程
    Create proc UpdateImage (@UpdateImage Image)
    As
    Insert Into test (FImage) values (@UpdateImage)
    GO 
    OpenFileDialog openfileDialog = new OpenFileDialog();
    openfileDialog.Filter = "Picture Files(*.jpg)|*.jpg|Bmp(*.bmp)|*.bmp|All Files(*.*)|*.*";
    FileStream fileStream;
    openfileDialog.ShowDialog();
    string filepath = openfileDialog.FileName;
    if (filepath.Trim() != "")
    {
      //获得图象并把图象转换为byte[]
      fileStream = new FileStream(filepath, FileMode.Open,  FileAccess.Read);
      byte[] photoArray = new byte[(int)fileStream.Length];
      fileStream.Read(photoArray, 0, photoArray.Length);
      fileStream.Close();
      try
      {
         pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
         pictureBox1.Image = System.Drawing.Image.FromFile(filepath);
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
      //连接数据库
      SqlConnection myConnection = new SqlConnection();
      myConnection.ConnectionString ="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
      SqlCommand mySqlCommand = new SqlCommand("UpdateImage", myConnection);
      mySqlCommand.CommandType = CommandType.StoredProcedure;
      mySqlCommand.Parameters.Add("@UpdateImage", SqlDbType.Image);
      mySqlCommand.Parameters["@UpdateImage"].Value = photoArray;
      //如果不使用存储过程,可以把上面四句代码改为:
      //string strSql="Insert into test(FImage) values(@FImage)";
      //SqlCommand cmd=new SqlCommand(strSql,conn);
      //cmd.Parameters.Add("@FImage",SqlDbType.Image);
      //cmd.Parameters["@FImage"].Value=photoArray;
      myConnection.Open();
      mySqlCommand.ExecuteNonQuery();
      myConnection.Close();
    }
    //以下是从数据库读取图片
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
    string strSql = "select FImage from test where id=1";
    SqlCommand cmd = new SqlCommand(strSql, conn);
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    reader.Read();
    MemoryStream ms = new MemoryStream((byte[])reader["FImage"]);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
    reader.Close();
    conn.Close();
    pictureBox1.Image = image;