• c#读写数据库图片


    c#读写数据库图片

    //将SQL server2000中保存的图像显示在Picture中
      private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
      {
       byte[] buffByte = null;
       string comm = @"select img from table1 where id = " + this.listBox1.SelectedValue ;
       this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
       this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
       this.sqlCommand1.CommandText = comm;
       this.sqlCommand1.Connection = this.sqlConnection1 ;
       this.sqlConnection1.Open();
       System.Data.SqlClient.SqlDataReader rd = this.sqlCommand1.ExecuteReader();
       while (rd.Read())
       {
        buffByte = ((byte[])rd[0]);
       }
       rd.Close();
       this.sqlConnection1.Close();
       //将图像的字节数组放入内存流
       System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
       //通过流对象建立Bitmap
       System.Drawing.Bitmap bmp = new Bitmap(ms);
       this.pictureBox1.Image = bmp;
      }
      
      //将图像保存到SQL server2000的Image字段中
      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 = buffByte;
        this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
        this.sqlCommand1.Parameters[1].Value =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();
       }
    }

  • 相关阅读:
    C#中如何禁止WindowsMediaPlayer双击全屏显示
    .NET中的泛型概述
    c# Windows服务管理
    C:Program不是内部或外部命令,也不是可运行的程序或批处理文件。
    Wireshark教程之二:Wireshark捕获数据分析
    Wireshark教程之一:认识Wireshark界面
    利用windows服务实现整点报时功能
    在windows服务中使用定时器
    flickity:支持触摸滑动,响应迅速的幻灯片轮播插件
    无法定位 Local Database Runtime 安装。请验证 SQL Server Express 是否正确安装以及本地数据库运行时功能是否已启用。
  • 原文地址:https://www.cnblogs.com/Tonyyang/p/768717.html
Copyright © 2020-2023  润新知