Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write Binary in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要讲解了"C#怎么读写Binary",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#怎么读写Binary"吧!

关于Binary方式

Binary和文本方式的区别,主要是在一些特殊字符的处理。

由于在不同的系统中,文本中用于显示格式的控制符编码是不同的。比如windows 的文本回车是0x13 0x10两个字符,而unix是0x10。如果是以文本方式打开,就会做一些转换。这样就不能保证从文件中读取到的内容和原来的完全一致,但能保证输出文本是显示的正确。而以Binary方式打开,内容完全一致,但在不同系统上输出可能不一样。

如果你读取的内容需要由系统显示,***用文本方式(由于很多C/C++编译器的输出函数能适用多种格式,所以用Binary方式通常也不会有问题)。如果要保证读取的和文件一致,就一定要用Binary方式。

C#编程实例-读写Binary

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(); } }

C#编程实例-读写Binary 调用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(); }

C#编程实例-读写Binary 调用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#怎么读写Binary"的内容了,经过本文的学习后,相信大家对C#怎么读写Binary这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report