将图片写入数据库和读取
来源:互联网 发布:软件实施工程师要求 编辑:程序博客网 时间:2024/06/05 02:53
在C#中将图片写入数据库并读取出来.源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace 将图片写入数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//用格式转换器直接将图片转换成二进制
ImageConverter img = new ImageConverter();
byte[] imgdate = (byte[])img.ConvertTo(this.pictureBox1.Image, typeof(byte[]));
using (SqlConnection conn = new SqlConnection(Connstr))
{
SqlCommand comm = conn.CreateCommand();
conn.Open();
comm.CommandText = string.Format("insert into myimage(imgdate) values(@imgdate)");
comm.Parameters.Add("@imgdate", SqlDbType.Image).Value = imgdate;
comm.ExecuteNonQuery();//要用参数写入
MessageBox.Show("Access");
}
}
private string Connstr = @"server=./SQLEXPRESS;DATABASE = TEST;INTEGRATED SECURITY = TRUE";
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openfd = new OpenFileDialog();
if (openfd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openfd.FileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Connstr))
{
SqlCommand comm = conn.CreateCommand();
conn.Open();
comm.CommandText = string.Format("select imgdate from myimage where imgid = {0}", 1);
SqlDataReader read = comm.ExecuteReader();
if (read.Read())
{
byte[] imgdate = (byte[])read["imgdate"];
//用内存流实现
MemoryStream ms = new MemoryStream(imgdate);
this.pictureBox1.Image = Image.FromStream(ms);
//用格式转换器实现
//ImageConverter ic = new ImageConverter();
//this.pictureBox1.Image =(Image)ic.ConvertFrom(imgdate);
}
read.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace 将图片写入数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//用格式转换器直接将图片转换成二进制
ImageConverter img = new ImageConverter();
byte[] imgdate = (byte[])img.ConvertTo(this.pictureBox1.Image, typeof(byte[]));
using (SqlConnection conn = new SqlConnection(Connstr))
{
SqlCommand comm = conn.CreateCommand();
conn.Open();
comm.CommandText = string.Format("insert into myimage(imgdate) values(@imgdate)");
comm.Parameters.Add("@imgdate", SqlDbType.Image).Value = imgdate;
comm.ExecuteNonQuery();//要用参数写入
MessageBox.Show("Access");
}
}
private string Connstr = @"server=./SQLEXPRESS;DATABASE = TEST;INTEGRATED SECURITY = TRUE";
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openfd = new OpenFileDialog();
if (openfd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openfd.FileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Connstr))
{
SqlCommand comm = conn.CreateCommand();
conn.Open();
comm.CommandText = string.Format("select imgdate from myimage where imgid = {0}", 1);
SqlDataReader read = comm.ExecuteReader();
if (read.Read())
{
byte[] imgdate = (byte[])read["imgdate"];
//用内存流实现
MemoryStream ms = new MemoryStream(imgdate);
this.pictureBox1.Image = Image.FromStream(ms);
//用格式转换器实现
//ImageConverter ic = new ImageConverter();
//this.pictureBox1.Image =(Image)ic.ConvertFrom(imgdate);
}
read.Close();
}
}
}
}
- 将图片写入数据库和读取
- 将图片写入数据库和读取
- 从数据库中读取图片文件和将图片和写入到数据库中
- Mysql--将图片写入数据库
- mysql将图片写入数据库
- php实现将文件写入数据库、从数据库读取文件(防止图片上传漏洞)
- android写入数据库、读取sqlite中的图片
- php将图片写入数据库并读出
- 将图片写入数据库并显示出来
- 从数据库读取和写入图片(包含调用相册和相机)
- c#大文件读取和写入数据库
- 从数据库中读取和写入文件
- C#大文件读取和写入数据库
- SQLite数据库的数据读取和写入
- 数据库并行读取和写入(Python实现)
- 将上传图片打上防伪图片水印并写入数据库
- 将上传图片打上防伪图片水印并写入数据库
- 将上传图片打上防伪图片水印并写入数据库
- 搜索引擎Nutch源代码研究之一+网页抓取(2)
- DataTable的一些介绍
- oracle01_sql
- jquey生成一维二维的插件
- MINI2440Linux驱动01-mini2440_adc
- 将图片写入数据库和读取
- hdu 1142 A Walk Through the Forest
- ARCHIVELOG模式下用户管理恢复临时文件!
- c++0x的新特性:lambda表达式
- WPF游戏编程--2D人物动画
- 在Emacs中使用gdb调试程序
- Ubuntu Linux系统下安装搜狗输入法
- SQL重复记录处理(查找,过滤,删除)
- 字符串操作函数的实现【笔试面试经常考的,这里都有了】