C# 怎么读写 Access数据库的OLE类型字段?

用C#怎么样才可以读取和写入OLE类型的Access数据库字段啊?我捣鼓了半天都没弄出来,急啊?!~我的QQ:928818821... 用C#怎么样才可以读取和写入OLE类型的Access数据库字段啊?我捣鼓了半天都没弄出来,急啊?!~
我的QQ:928818821
展开
 我来答
一点设计演示
高粉答主

推荐于2016-06-26 · 职场/教育各类模板,衷于原创
一点设计演示
采纳数:874 获赞数:83628

向TA提问 私信TA
展开全部
(一)把文件内容写入Access数据库的OLE对象字段中:
if (File.Exists(txtBrow.Text) != false) // 文本框txtBrow中内容为文件路径及文件名
{
//获取文件后缀
FileInfo p = new FileInfo(txtBrow.Text.Trim());
F_str_Type = p.Extension.ToLower();
if (F_str_Type.Length > 5)
{
MessageBox.Show("不可识别的文件格式,请重新确认!","警告");
return;
}
//判断文件大小
if (p.Length == 0)
{
MessageBox.Show("文件的大小为“0”,不能保存!", "警告");
return;
}
//创建文件对象以打开的形式读取文件
FileStream sFileStream = new FileStream(txtBrow.Text, FileMode.Open);
//分配数组大小
byte[] bFile = new byte[sFileStream.Length];
//将文件内容读进数组
sFileStream.Read(bFile, 0, (int)sFileStream.Length);
//关闭文件对象
sFileStream.Close();
//查找文档类别号
OleDbDataReader topicread = SaveConn.GetReader("select File_ID from FileTopic where File_Topic='" + cbbTopic.Text.Trim() + "'");
//Read()方法用来读取OleDbDataReader对象中的记录
topicread.Read();
T_int_Topic=(int)topicread["File_ID"];
OleDbConnection conn = SaveConn.GetConn();
conn.Open();
OleDbCommand com = conn.CreateCommand();
com.CommandText = "insert into FileTitle(File_CodeID,File_TopicID,File_Title,File_Time,File_Save,File_Name,File_Type) values(@File_CodeID,@File_TopicID,@File_Title,@File_Time,@File_Save,@File_Name,@File_Type)";
com.Parameters.AddWithValue("@File_CodeID", txtCode.Text.Trim()); //文档编号
com.Parameters.AddWithValue("@File_TopicID", T_int_Topic); //文档类别号
com.Parameters.AddWithValue("@File_Title", txtTitle.Text.Trim()); //文档标题
com.Parameters.AddWithValue("@File_Time", txtTime.Text.Trim()); //存储时间
com.Parameters.AddWithValue("@File_Save", bFile); //文档内容
com.Parameters.AddWithValue("@File_Name", txtName.Text.Trim()); //经手人
com.Parameters.AddWithValue("@File_Type", F_str_Type); //文档类型
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("文档资料添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

(二)双击DataGridView控件某一行,显示文档内容
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
string F_str_Open;
//如果不存在就创建Temp文件夹
if (Directory.Exists(Application.StartupPath + @"/Temp") == false)
{
Directory.CreateDirectory(Application.StartupPath + @"/Temp");
}
try
{
OleDbDataReader sqlread = SaveConn.GetReader("select File_Save,File_Type from FileTitle where File_CodeID='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
sqlread.Read();
//判断文件是否已存在.
if (File.Exists(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]))
{
//如存在则删除
File.Delete(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]);
}
byte[] bFile=(byte[])sqlread[0];
//创建文件对象
System.IO.FileStream sFileStream = new System.IO.FileStream(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"], System.IO.FileMode.Create);
//将数组的内容写进文件
sFileStream.Write(bFile, 0, bFile.Length);
//关闭文件
sFileStream.Close();
Process p = new Process();
p.StartInfo.FileName = Convert.ToString(Application.StartupPath + @"/Temp/Temp" + sqlread["File_Type"]);
p.Start();
p.Close();
sqlread.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
rightmin
2009-05-08 · TA获得超过4124个赞
知道大有可为答主
回答量:2199
采纳率:0%
帮助的人:1839万
展开全部
假设OLE字段CMenu:

byte[] buff=你程序的数据;

OleCom.CommandText = "update [Catalog] set [CMenu] = @olevalue";

OleDbParameter olep = new OleDbParameter("@olevalue", OleDbType.LongVarBinary, buff.Length);
olep.Value = buff;
OleCom.Parameters.Add(olep);
OleCom.ExecuteNonQuery();执行

读取和其他的字段读取方式一样,返回的东西用byte[]进行强制类型转换
byte[] buff = (byte[])dr[1];
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式