如何将字符串转换为字节以存储在数据库中,然后在网格视图中将其作为字节串转换? [英] how to converting string to byte to store in database and then retrieving it in grid view as byte to string?

查看:90
本文介绍了如何将字符串转换为字节以存储在数据库中,然后在网格视图中将其作为字节串转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的文本框值存储为数据库中的字节,我编写的方法转换为byte&是这样的

 dpc.AddParameter( new  DataParam(  @ Template_Content,fr.GetBytes(txtTemplatebody),DataParam.DataParamTypes.VarBinary,-1, 8000 )); 



它有效。但是当我检索数据并将其绑定到网格中时,我需要将特定列中的字节转换为字符串,我浪费了很多时间。但我不能这样做。请帮帮我在此先感谢。

解决方案

要从字符串转换为字节数组,从字节数组转换为字符串,您应该使用使用相同编码的pair方法,例如下一个例子:



1.从字符串字节数组

  byte  [] resultArray = System.Text.Encoding.UTF8.GetBytes(stringValue) ; 



2.从字节数组字符串

  string  stringResult = System.Text.Encoding.UTF8.GetString(byteArray); 


I tried to store my textbox value as byte in database, i wrote method to convert as byte & did it like this

dpc.AddParameter(new DataParam("@Template_Content", fr.GetBytes(txtTemplatebody), DataParam.DataParamTypes.VarBinary, -1, 8000));


it works gud. but when i retrive data and bind it in grid i need to convert byte in particular column as string, i wasted lots of time in it. but i couldn't do it. please do help me. Thanks in advance.

解决方案

For converting from string to bytes array and from bytes array to string you should use pair methods, that are using the same encoding, like in the next examples:

1.From string to byte array:

byte[] resultArray = System.Text.Encoding.UTF8.GetBytes(stringValue);


2.From byte array to string:

string stringResult = System.Text.Encoding.UTF8.GetString(byteArray);


这篇关于如何将字符串转换为字节以存储在数据库中,然后在网格视图中将其作为字节串转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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