C#从SQL server数据库中读取l图片和存入图片
来源:互联网 发布:博罗县政府网络问政 编辑:程序博客网 时间:2024/05/16 10:05
本实例主要介绍如何将图片存入数据库。将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
string fullpath =openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
byte[] imagebytes =new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//打开数据库
SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");
con.Open();
SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);
com.Parameters.Add("ImageList", SqlDbType.Image);
com.Parameters["ImageList"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
}
}
本实例主要介绍如何从数据库中把图片读出来。实现本实例主要是利用SqlDataReader从数据库中把Image字段值读出来,赋给一个byte[]字节数组,然后使用MemoryStream类与Bitmap把图片读取出来。主要代码如下:
private void button1_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
//打开数据库
SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");
con.Open();
SqlCommand com = new SqlCommand("select top 1* from tb_09", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(1);
}
dr.Close();
com.Clone();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
pictureBox1.Image = bmpt;
}
本实例主要介绍如何只允许输入指定图片格式。用OpenFileDialog控件打开图片文件,只要将OpenFileDialog控件的Filter属性指定为特定的图片格式即可。例如,打开.bmp文件的图片,主要代码如下:
this.openFileDialog1.Filter = "bmp文件(*.bmp)|*.bmp";
在用pictureBox控件输入图片时,要想统一图片大小,只需把控件的SizeMode属性值设为StretchImage即可,StretchImage值表示图像的大小将调整为控件的大小。这样,图片的大小就统一了。
- C#从SQL server数据库中读取l图片和存入图片
- 从SQL 数据库中读取和存入图片问题
- 用c#向SQL Server中存储图片并且再从数据库中读取图片
- c#向SQL Server中存储图片并且再从数据库中读取图片
- C#中图片存入SQL数据库
- 【转】C# 如何将 图片直接存入SQL数据库中
- C#图片存入数据库
- 从数据库中读出图片,存入硬盘
- SQL Server中存储和读取图片
- SQL Server中存储和读取图片
- C# 将图片存入数据库中
- C#l图片保存到数据库和从数据库读取图片并显示
- SQL Server数据库添加图片读取图片
- 从数据库中字段为byte的图片读取处理存入磁盘
- C#将照片或图片转化为byte[]存入数据库,从数据库中读照片
- C#将图片存入SQLServer数据库并读取
- C#用LINQ to SQL把PictureBox中的图片存入数据库和取出数据库中的图片并显示在PictureBox中
- C#从数据库中读取二进制数据,并显示图片
- 高雅的数学表示
- scikit-learn初探:多种回归方法建立预测电力输出模型(一)
- path和classpath的区别
- GOF设计模式
- UltraEdit怎么批量删除指定字符所在的行
- C#从SQL server数据库中读取l图片和存入图片
- Android Studio生成和引用aar
- Linux命令简介—nl
- zookeeper 启动错误
- 关于服务器的一些优化
- 内部类
- UITableView上编辑UITextView
- android shape的使用
- Mac卸载MySQL