• C#保存图片到数据库并读取显示图片的方法



    private void button2_Click_1(object sender, System.EventArgs e)

    {

    string pathName;

    if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)

    {

    pathName = this.openFileDialog1.FileName;

    System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);

    this.pictureBox1.Image = img;

    //将图像读入到字节数组

    System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

    byte[] buffByte = new byte[fs.Length];

    fs.Read(buffByte,0,(int)fs.Length);

    fs.Close();

    fs = null;

    //建立Command命令

    string comm = @"Insert into table1(img,name) values(@img,@name)";

    this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();

    this.sqlCommand1.CommandType = System.Data.CommandType.Text ;

    this.sqlCommand1.CommandText = comm;

    this.sqlCommand1.Connection = this.sqlConnection1 ;

    //创建Parameter

    this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);

    this.sqlCommand1.Parameters[0].Value = http://www.jb51.net/ianakin/archive/2012/02/02/buffByte;

    this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);

    this.sqlCommand1.Parameters[1].Value =http://www.jb51.net/ianakin/archive/2012/02/02/pathName.Substring(pathName.LastIndexOf("\")+1);

    try

    {

    this.sqlConnection1.Open();

    this.sqlCommand1.ExecuteNonQuery();

    this.sqlConnection1.Close();

    }

    catch(System.Exception ee)

    {

    MessageBox.Show(ee.Message );

    }

    buffByte = null;

    this.FillListBox();

    }


    读取:

    从数据库读图片到picturebox


    SqlConnection conn=new SqlConnection(@"data source=chenyuming2004VSdotNET;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;

  • 相关阅读:
    前端性能优化
    技术从业者的未来(二)
    微服务架构
    SpringCloud 微服务最佳开发实践
    架构师之路
    SpringBoot开发秘籍
    架构设计方法论
    消息架构的设计难题以及应对之道
    SpringCloud 中如何防止绕过网关请求后端服务?
    微服务架构授权是在网关做还是在微服务做?
  • 原文地址:https://www.cnblogs.com/jf-guo/p/6610426.html
Copyright © 2020-2023  润新知