谢谢上面几位高手的热心帮助!
由于小弟是初次学习C#,不是很熟悉!我已经通过查找可以知道可以把有格式将文本和图片转化成长二进制存放在access数据库中的ole类型字段中。但是请教各位高手们告诉我
如何在C#中将文本和图片转换成长二进制存放在access数据库中,又如何从数据库中的ole类型字段中读出数据!!!
再次恳请给为高手给我指导!不胜感激!!!
由于小弟是初次学习C#,不是很熟悉!我已经通过查找可以知道可以把有格式将文本和图片转化成长二进制存放在access数据库中的ole类型字段中。但是请教各位高手们告诉我
如何在C#中将文本和图片转换成长二进制存放在access数据库中,又如何从数据库中的ole类型字段中读出数据!!!
再次恳请给为高手给我指导!不胜感激!!!
解决方案 »
- 安装Windows服务如何找出服务执行文件名
- 怎样判断TreeView是否有选中项
- 如何让封装在dll里的自定义控件出现在VS的工具栏上
- 求助:Unable to Validate Data
- 简单问题:C#中byte和bool类型哪个占内存更少?
- 为何在WinFormr的DataGrid中选择一行后,点删除按钮不能删除数据库的记录呢?
- 100分求教各位高手这个存储过程要怎样写!!?????????????不胜感激!!!!!!!!!!!!!!!!!!!!
- 齐齐哈尔软件公司招聘C程序员吗?
- 用接口、还是抽象类实现调用对应编辑窗口Add()、Delete()、Update()方法。
- mm急呀!DataGrid如何保存刚输入的行?
- 懂日语的进 谢谢
- 迷茫啊!是做美工,还是学C#转做程序啊?
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();
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)
{
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();
}
}