由于小弟是初次学习C#,不是很熟悉!
我已经通过查找可以知道可以把有格式将文本和图片转化成长二进制存放在access数据库中的ole类型字段中。但是请教各位高手们告诉我
如何在C#中将文本和图片转换成长二进制存放在access数据库中,又如何从数据库中的ole类型字段中读出数据!!!
再次恳请给为高手给我指导!不胜感激!!!
7 个解决方案
#1
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185154
#2
一般不采用这样的做法,而是将图片上传,然后将图片名放在数据库里,然后再从数据中读
#3
转为byte类型后存入Image字段。
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
#4
//将C#数据以长二进制数据保存于Access数据库northwind.mdb
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add("Test","2.jpg");
}
public static void Add(string categoryName, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("INSERT INTO 类别(类别名称, 图片) VALUES (@CategoryName, @Picture)", cn);
cmd.Parameters.Add("@CategoryName", OleDbType.VarChar,15).Value = categoryName;
cmd.Parameters.Add("@Picture", OleDbType.Binary, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add("Test","2.jpg");
}
public static void Add(string categoryName, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("INSERT INTO 类别(类别名称, 图片) VALUES (@CategoryName, @Picture)", cn);
cmd.Parameters.Add("@CategoryName", OleDbType.VarChar,15).Value = categoryName;
cmd.Parameters.Add("@Picture", OleDbType.Binary, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
#5
//从Access数据库northwind.mdb取出长二进制数据
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("Select 类别ID,图片 FROM 类别 where 类别名称='Test'", cn);
FileStream fs;
BinaryWriter bw;
//缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval = 0;
//BLOB输出的起始位置
long startIndex = 0;
string id = "";
cn.Open();
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while(dr.Read())
{
id = dr.GetValue(0).ToString();
fs = new FileStream(id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
while(retval == bufferSize)
{
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
}
bw.Write(outByte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();
}
dr.Close();
cn.Close();
}
}
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("Select 类别ID,图片 FROM 类别 where 类别名称='Test'", cn);
FileStream fs;
BinaryWriter bw;
//缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval = 0;
//BLOB输出的起始位置
long startIndex = 0;
string id = "";
cn.Open();
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while(dr.Read())
{
id = dr.GetValue(0).ToString();
fs = new FileStream(id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
while(retval == bufferSize)
{
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
}
bw.Write(outByte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();
}
dr.Close();
cn.Close();
}
}
#6
谢谢以上各位的热心帮助!!!
#7
你好,我想问下楼主说可以用把有格式将文本和图片转化成长二进制存放在access数据库中的ole类型字段中。那用什么办法把文本和图片转化成长二进制?
#1
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185154
#2
一般不采用这样的做法,而是将图片上传,然后将图片名放在数据库里,然后再从数据中读
#3
转为byte类型后存入Image字段。
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
#4
//将C#数据以长二进制数据保存于Access数据库northwind.mdb
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add("Test","2.jpg");
}
public static void Add(string categoryName, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("INSERT INTO 类别(类别名称, 图片) VALUES (@CategoryName, @Picture)", cn);
cmd.Parameters.Add("@CategoryName", OleDbType.VarChar,15).Value = categoryName;
cmd.Parameters.Add("@Picture", OleDbType.Binary, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add("Test","2.jpg");
}
public static void Add(string categoryName, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("INSERT INTO 类别(类别名称, 图片) VALUES (@CategoryName, @Picture)", cn);
cmd.Parameters.Add("@CategoryName", OleDbType.VarChar,15).Value = categoryName;
cmd.Parameters.Add("@Picture", OleDbType.Binary, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
#5
//从Access数据库northwind.mdb取出长二进制数据
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("Select 类别ID,图片 FROM 类别 where 类别名称='Test'", cn);
FileStream fs;
BinaryWriter bw;
//缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval = 0;
//BLOB输出的起始位置
long startIndex = 0;
string id = "";
cn.Open();
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while(dr.Read())
{
id = dr.GetValue(0).ToString();
fs = new FileStream(id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
while(retval == bufferSize)
{
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
}
bw.Write(outByte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();
}
dr.Close();
cn.Close();
}
}
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb");
OleDbCommand cmd = new OleDbCommand("Select 类别ID,图片 FROM 类别 where 类别名称='Test'", cn);
FileStream fs;
BinaryWriter bw;
//缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval = 0;
//BLOB输出的起始位置
long startIndex = 0;
string id = "";
cn.Open();
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while(dr.Read())
{
id = dr.GetValue(0).ToString();
fs = new FileStream(id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
while(retval == bufferSize)
{
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
}
bw.Write(outByte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();
}
dr.Close();
cn.Close();
}
}
#6
谢谢以上各位的热心帮助!!!
#7
你好,我想问下楼主说可以用把有格式将文本和图片转化成长二进制存放在access数据库中的ole类型字段中。那用什么办法把文本和图片转化成长二进制?