首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

SQLServer数据库中图片的惠存和读取

2012-10-19 
SQLServer数据库中图片的存入和读取SQLServer数据库中图片的存入和读取。1、数据库中存放图片的字段是什么类

SQLServer数据库中图片的存入和读取
SQLServer数据库中图片的存入和读取。

1、数据库中存放图片的字段是什么类型的?

2、把图像转化为二进制流存入数据库的方法?

3、把二进制流转化图片的方法?

[解决办法]
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "イメージファイル(*.gif,*.jpg,*.jpeg,*.bmp,*.wmf,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png";
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo f = new FileInfo(ofd.FileName);
file = ofd.FileName;
this.pictureBox1.Image = Image.FromFile(file);
}


可以定义Image 类型 ,数据库中也有这个类型,就像其他类型一样操作就行了
private Image iMAGE;
public Image IMAGE
{
get { return iMAGE; }
set { iMAGE = value; }
}

 obj.IMAGE = pictureBox1.Image;

[解决办法]
1. image or binary

SQL图片存取


C# code
using System;using System.Data;using System.Data.SqlClient;using System.IO;// 下面是从数据库取出图片try{    string getPhoto = @"select photo from gch_Customer_Photo where customerID=1234";        DataSet ds = new DataSet();    sqlDataAdapter1 = new SqlDataAdapter(getPhoto, sqlConnection1);    sqlDataAdapter1.Fill(ds);    DataRow row = ds.Tables[0].Rows[0];    byte[] bPhoto = new byte[0];    bPhoto = (byte[])row["photo"];    //int arraySize = bPhoto.GetUpperBound(0);    MemoryStream memstr = new MemoryStream(bPhoto);    pictureBox1.Image = Image.FromStream(memstr, true);    existPhoto==true}catch(Exception ex){    Console.WriteLine(ex.ToString());}// 下面是将图片存入数据库OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = "*.bmp;*.jpg;*.gif|*.bmp;*.jpg;*.gif;*.jpeg";if(ofd.ShowDialog()==DialogResult.OK){    string filePath = ofd.FileName;    FileInfo imageFile = new FileInfo(filePath);    if(imageFile.Length > 204800)    {        Console.WriteLine("图片大小最好不要过大!");    }    pictureBox1.Image = Image.FromFile(filePath);    if(existPhoto==true)    { //如果之前已有图片,可以考虑删除了旧的先        string deleteOld = @"delete from gch_Customer_Photo where customerID=1234";        SqlCommand sqlcmmd = new SqlCommand(deleteOld,sqlConnection1);        sqlcmmd.ExecuteNonQuery();    }    string getAllPhotos = @"select customerID, photo from gch_Customer_Photo";    DataSet ds = new DataSet();    sqlDataAdapter1 = new SqlDataAdapter(getAllPhotos,sqlConnection1);    sqlDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey;    FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);    byte[] bPhoto= new byte[fs.Length];    fs.Read(bPhoto, 0, System.Convert.ToInt32(fs.Length));    fs.Close();    sqlDataAdapter1.Fill(ds);                        DataRow oneRow = ds.Tables[0].NewRow();    oneRow["customerID"] = 1234;    oneRow["photo"] = bPhoto;    ds.Tables[0].Rows.Add(oneRow);    sqlDataAdapter1.Update(ds);        Console.WriteLine("图片入库成功!");}
[解决办法]
1. 数据库存放的是 Image 类型
2. 用 byte[] data = File.ReadAllBytes(图片路径)
3. byte[] -> MemoryStream -> Bitmap.FromStream

[解决办法]
我一般都说把图片存在本地的文件夹内,然后在数据库里存路径
[解决办法]
探讨
我一般都说把图片存在本地的文件夹内,然后在数据库里存路径

[解决办法]
顺便请教下各位大侠,如果是考勤机和二代身份证,读出来的头像图片是怎样存储的,存储在本地的什么位置???


你用的什么写的 C# winfrom程序???



[解决办法]
我也是这样做的,这样不仅简单方便还便于管理。
探讨
我一般都说把图片存在本地的文件夹内,然后在数据库里存路径



[解决办法]
过程重要,结果更重要!
探讨
SQLServer数据库中图片的存入和读取。

1、数据库中存放图片的字段是什么类型的?

2、把图像转化为二进制流存入数据库的方法?

3、把二进制流转化图片的方法?

[解决办法]
探讨
1. 数据库存放的是 Image 类型
2. 用 byte[] data = File.ReadAllBytes(图片路径)
3. byte[] -> MemoryStream -> Bitmap.FromStream

[解决办法]
探讨

引用:
1. 数据库存放的是 Image 类型
2. 用 byte[] data = File.ReadAllBytes(图片路径)
3. byte[] -> MemoryStream -> Bitmap.FromStream


二进制存储更安全。

[解决办法]
各有所长。那种都行,只要合适。
[解决办法]
图片的插入和读取
[解决办法]
围观,如果用得多,二进制存到数据库会越来越多,查询的时候不晓得啥子效果
[解决办法]
1、图片存入数据库
本实例主要介绍如何将图片存入数据库。将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下:
/// <summary>
/// 将图片以二进制读入数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);database=zy_info;integrated security=true");
con.Open();
SqlCommand com = new SqlCommand("insert into tb_text values(@guatu)", con);
//"update yijing set guatu='"+
com.Parameters.Add("guatu", SqlDbType.Image);
com.Parameters["guatu"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
}
}


2、图片读出数据库
/// <summary>
/// 读出二进制图片数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
//打开数据库
SqlConnection con = new SqlConnection("server=(local);database=zy_info;integrated security=true");
con.Open();
SqlCommand com = new SqlCommand("select top 1* from tb_text", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr["guatu"];
}
dr.Close();
com.Clone();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
//dr.Close();
//com.Clone();
//con.Close();
}
这个可以成功的!
[解决办法]
SQLServer数据库中图片的存入和读取。

1、数据库中存放图片的字段是什么类型的?
image

2、把图像转化为二进制流存入数据库的方法?
byte[]数组 可以用File.ReadAllBytes()或FileStream的Read()等方法
然后Insert Into table value(@image)...



3、把二进制流转化图片的方法?
同样的读入byte[] 实例MemoryStream 然后用FromStream()
[解决办法]
貌似可以打包吧 ,我记得Access也是这样,存储图片就是直接用
[解决办法]
1.2000 image ,2005是binary或者varbinary
2.FileStream-fs-> BinaryReader br = new BinaryReader(fs)或者fs.Read(byt[],0,length)
3.MemoryStream ms=new MemoryStream(datatable["图片"])--Image im=Image.FromStream(ms);
[解决办法]

探讨
围观,如果用得多,二进制存到数据库会越来越多,查询的时候不晓得啥子效果

[解决办法]
刚做过这个模块、其实也不难 有需要找我要把

热点排行