C#实现读写Binary
发布日期:2021-05-07 10:36:28 浏览次数:20 分类:原创文章

本文共 1827 字,大约阅读时间需要 6 分钟。

 


本文给出一个用 C# 编程实现读写 Binary 的实例代码,对于初学者来说是个不可多得的参考性文章……


以下是引用片段:
  //返回blob数据
  public MemoryStream getBlob(string SQL)
  ...{
  try
  ...{
  Db_Conn();
  cmd = new OleDbCommand(SQL, Conn);
  cmd.CommandType = CommandType.Text;//是sql
  OleDbDataReader Rs = cmd.ExecuteReader();
  if (Rs.Read()) //循环到下一条记录
  ...{
  if (!(Rs.GetValue(0) is System.DBNull))
  ...{
  byte[] image_bytes = (byte[])Rs.GetValue(0);
  MemoryStream ms = new MemoryStream(image_bytes);
  return ms;
  }
  else
  return null;
  }
  else
  return null;
  }
  finally
  ...{
  this.close();
  }
  }
  //设置blob
  public bool SetBlob(string SQL, MemoryStream Ms)
  ...{
  try
  ...{
  Db_Conn();
  cmd = new OleDbCommand(SQL, Conn);
  cmd.CommandType = CommandType.Text;//是sql
  int n=Convert.ToInt32(Ms.Length.ToString());
  Ms.Position = 0;
  byte[] pReadByte = new Byte[n];
  Ms.Read(pReadByte, 0, n);
  cmd.Parameters.Add("BLOB", OleDbType.Binary).Value = pReadByte;
  cmd.ExecuteNonQuery();
  return true;
  }
  catch (Exception ex)
  ...{
  MessageBox.Show("错误:因" + ex.Message + ",无法执行:" + SQL);
  return false;
  }
  finally
  ...{
  this.close();
  }
  }



  调用 getBlob


以下是引用片段:
  String sqlStr = "select content from dp where id=" + ID;//content为dp中的BLOB字段,ID为主键
  MemoryStream ms = DBClass.getBlob(sqlStr);
  if (ms == null)
  richTextBox.Clear();
  else
  ...{
  if (ms.Length > 0)
  ...{
  ms.Position = 0;
  try
  ...{
  richTextBox.LoadFile(ms, RichTextBoxStreamType.RichText);
  }catch...{
  richTextBox.LoadFile(ms, RichTextBoxStreamType.PlainText);
  }
  }else
  richTextBox.Clear();
  }



  调用setBlob


以下是引用片段:
  String sqlStr = "update dp set content=:BLOB where id=" + ID;
  MemoryStream ms = new MemoryStream();
  richTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
  if (!DBClass.SetBlob(sqlStr, ms))
  ...{
  MessageBox.Show("保存失败");
  }  

上一篇:随机迷宫生成算法浅析
下一篇:C#数据库操作的三种经典用法

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年05月10日 17时57分57秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Nmap端口服务 之 CentOS7 关于启动Apache(httpd)服务、telnet服务、smtp服务、ftp服务、sftp服务、snmp服务 2023-01-23
PHP系列:PHP 基础编程 2(时间函数、数组---实现登录&注册&修改) 2023-01-23
PHP系列:使用PHP实现登录注册功能的完整指南 2023-01-23
Python&aconda系列:cmd/powershell/anaconda prompt提示“系统找不到指定的路径”(亲测有效) 2023-01-23
Python&aconda系列:conda踩坑记录2.UnsatisfiableError: The following specifications were found to be incompa 2023-01-23
Python&aconda系列:Jupyter Notebook快速上手、深度学习库PyTorch安装 2023-01-23
Python&aconda系列:(W&L)Conda使用faiss-gpu报错及解决办法、安装numpy的坑、cmd执行Python脚本找不到第三方库、安装tensorflow-gpu时遇到的from 2023-01-23
python&anconda 系列:Pycharm在debug问题的N种解决方案(一般程序、web方向、人工智能方向) 2023-01-23
python&anconda系列(亲测有效):tensorflow AttributeError: ‘str’ object has no attribute ‘decode’ 2023-01-23
python&anconda系列:tf.keras.backend.get_session()和keras.backend.get_会话()返回不同的会话对象(待解答) 2023-01-23
"WARNING: Increasing RAM size to 1GB" and "Cannot set up guest memory 'xxx.ram': Invalid argument". 2023-01-23
#if 0 #elif 1 #else #endif 用法 2023-01-23
#include <gdiplus.h>出错 2023-01-23
$ajax({}).done 和 $ajax({}) success 区别 2023-01-23
'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) 2023-01-23
(反射+内省机制的运用)处理jdbc的结果集 2023-01-23
(反射+内省机制的运用)简单模拟spring IoC容器的操作 2023-01-23
(转)SQLServer全局变量 2023-01-23
(转)tomcat7.0 manager app和host manager web管理 2023-01-23
(转)【英雄会即时报道】五大CTO畅谈软件公司如何招聘技术人才 2023-01-23