使用GetBytes&则GetChars [英] Use GetBytes & GetChars

查看:53
本文介绍了使用GetBytes&则GetChars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



目标:



我想将重要数据保存为二进制



我的方法:



i使用以下代码转换RTF和String以将它们保存在SQL Server库中VarBinary字段:



Dim txt1 as Byte()

txt = System.Text.Encoding.UTF8.GetBytes(Test字符串)





并且使用此代码我可以将检索到的数据转换为字符串:



dim txt2 as string

txt2 = System.Text.Encoding.UTF8.GetChars(txt1)





问题:



我的应用程序设计是3层,函数返回DataTable,我可以正确获取数据,只要计算就转换它们没有任何问题正在代码中完成。但是当我想将DataTable分配给DataGridView.DataSource时,我只是因为VarBinary列而面临许多错误,最后显示一个图标,表示我应该在该列中加载图像,当然这些列实际上是字符串。 />


我的方法是否符合这个目标?

如何解决这个问题?



提前谢谢

hello

goal:

I want to save important data as binary

my approach:

i used following code to convert "RTF" and "String" to save them in SQL Server bank with "VarBinary" field:

Dim txt1 as Byte()
txt = System.Text.Encoding.UTF8.GetBytes("Test String")


and with this code i could convert retrieved data to string:

dim txt2 as string
txt2 = System.Text.Encoding.UTF8.GetChars(txt1)


problem:

my application design is 3 layer and a function return DataTable and i can get data correctly and convert them whithout any problem as long as calculation is being done just in codes. but when i want to assign DataTable to DataGridView.DataSource I face many errors just because of "VarBinary" column and finally shows an icon which indicated i were supposed to load image in that column, well of course those columns are string actually.

is my approach correct for that goal?
how can i solve this problem?

thank you in advance

推荐答案

mmbguide写道:
mmbguide wrote:


我的应用程序中的
用户能够发送彼此的消息但是,我想加密所有消息文本以禁止其他有权访问银行的人阅读


in my application users are able to send each other message but, I want to encrypt all the message text to forbidden from being read by others who has access to bank

编码无关加密。下一步,无论它是什么,你至少需要学习加密的基础知识。至少,你要理解这些想法。你可以从这里开始:

https://en.wikipedia.org/wiki/Cryptography [ ^ ],

首先, http://en.wikipedia.org/wiki/Public-key_cryptography [ ^ ],

https://en.wikipedia.org/wiki/Symmetric-key_algorithm [ ^ ],

https://en.wikipedia.org/wiki/Cryptographic_hash_function [ ^ ]。



.NET FC L几乎为您提供所需的一切:

https://msdn.microsoft.com/en-us/library/system.security.cryptography%28v=vs.110%29.aspx [ ^ ],
https:// msdn .microsoft.com / zh-cn / library / system.security.cryptography.asymmetricalgorithm%28v = vs.110%29.aspx [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm%28v=vs.110%29.aspx> https://msdn.microsoft.com/en -us /库/ system.security.cryptography.sym metricalgorithm%28v = vs.110%29.aspx [ ^ ],

...... :-)



你对编码的困惑是如此之大,我建议你至少学习文本表示和Unicode的主要概念:

http://en.wikipedia.org/wiki/Unicode [ ^ ],

http:// unicode。组织 [ ^ ],

http://unicode.org/faq/utf_bom.html [ ^ ],

https: //msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx [ ^ ]。



-SA

Encoding has nothing to do with cryptography. Do make a next step, no matter what it is, you need to learn at least the basics of cryptography. As a minimum, you to understand the ideas. You can start here:
https://en.wikipedia.org/wiki/Cryptography[^],
first of all, http://en.wikipedia.org/wiki/Public-key_cryptography[^],
https://en.wikipedia.org/wiki/Symmetric-key_algorithm[^],
https://en.wikipedia.org/wiki/Cryptographic_hash_function[^].

.NET FCL offers you virtually everything you may need:
https://msdn.microsoft.com/en-us/library/system.security.cryptography%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm%28v=vs.110%29.aspx[^],
… :-)

Your confusion with Encoding is so great that I advise you to learn at least main concepts of text representation and Unicode:
http://en.wikipedia.org/wiki/Unicode[^],
http://unicode.org[^],
http://unicode.org/faq/utf_bom.html[^],
https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx[^].

—SA


最简单的解决方案似乎是:如果是字符串,请将其保留为字符串。

在SQL Server表中使用NVARCHAR字段(知道Unicode字符,因此它正常工作)并使用参数化查询直接保存字符串。这样,没有转换,所以没有转换问题的机会,DataGridView也会正确显示字符串内容。



不要存储任何内容最合适的领域:它总是会导致问题。 c#字符串是SQL NVARCHAR字段。
The simplest solution would seem to be: if it's a string, leave it as a string.
Use a NVARCHAR field in your SQL Server table (that knows about Unicode characters, so it "just works") and save the string directly using a parameterized query. That way, there is no conversion, so no chance of conversion problems, and the DataGridView will display the string content correctly as well.

Don't store anything in a field other than the most appropriate: it always causes problems later. And a c# string is a SQL NVARCHAR field.


这篇关于使用GetBytes&amp;则GetChars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆