如何将字符串字节转换为bytes []并保存到数据库中

时间:2016-11-28 06:31:42

标签: c# asp.net sql-server json json.net

我有一些JSON数据,其中包含带字节的图像字段。我想将这些字节保存到数据库中,但我无法将字符串转换为字节数组。

{
    "trackid": "TRXh82URBNA2878934",
    "serviceid": "66",
    "category": "URBN",
    "name": "sachin",
    "description": "Sanitations Work",
    "location": "Greater Noida",
    "requesterlocation": "new delhi",
    "email": "sachin@gmail.com",
    "contact_number": "882379823",
    "latitude": "78.23",
    "longitude": "30.23",
    "locationtype": "urban",
    "sectorid": "12",
    "userid": "34",
    "filename": "IMG001",
    "extension": ".jpg",
    "contenttype": "image/jpeg",
    "image": "0xFFD8FFE13F5745786966000049492A000800000012000E01020020000000E60000000F01020020000000060100001001020020000000260100001201030001000000010000001A01050001000000460100001B010500010000004E01000028010300010000000200000031010200200000005601000032010200140000007601000013020300010000000200000020020400010000000000000021020400010000000000000022020400010000000000000023020400010000000000000024020400010000000100000025020200200000008A0100006987040001000000AA01000025880400010000002C0300004504000000000000000000000000000000000000000000000000000000000000000000004F50504F000000000000000000000000000000000000000000000000000000005839303039000000000000000000000000000000000000000000000000000000480000000100000048000000010000004D6564696154656B2043616D657261204170706C69636174696F6E0A00000000323031363A31303A32312030393A34383A323200000000000000000000000000000000000000000000000000000000000000000019009A82050001000000DC0200009D82050001000000E40200002288030001000000000000002788030001000000320000000090070004000000303232300390020014000000EC02000004900200140000000003000001910700040000000102030004920A0001000000140300000792030001000000020000000892030001000000FF000000099203000"
}

我在将字节字符串插入数据库时​​遇到错误。这是我的代码:

SqlCommand cmd = new SqlCommand("PROC_INSERT_CITIZENT_REQUEST_DOCUMENTS_FOR_WEB_API");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
Connection.Open();
cmd.Parameters.AddWithValue("@P_Cititzen_request_recno", oRequest.requestid);
cmd.Parameters.AddWithValue("@P_Category", oRequest.category);
cmd.Parameters.AddWithValue("@P_Filename", oRequest.filename);
cmd.Parameters.AddWithValue("@P_extension", oRequest.extension);
cmd.Parameters.AddWithValue("@P_contentType", oRequest.contenttype);
//byte[] newBytes = Convert.FromBase64String(oRequest.image);
cmd.Parameters.AddWithValue("@P_Doc_Data", Encoding.UTF8.GetBytes(oRequest.image));
cmd.Parameters.AddWithValue("@P_MSG", "");

j = cmd.ExecuteNonQuery();
if (j < 1)
{
    j = 0;
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,您的字符串不是Base64编码的,也不是UTF8编码的。它是一串十六进制数字。您需要一种从十六进制转换为字节的方法。这个主题已经在Stack Overflow上被覆盖了;见How do you convert Byte Array to Hexadecimal String, and vice versa?

然而,还有另一个问题:为了使用链接问题中的一个答案从十六进制字符串转换,&#34; 0x&#34;必须首先删除前缀,并且十六进制字符串的长度必须是偶数(因为十六进制数字是成对处理以产生字节)。在您的情况下,删除前缀后十六进制字符串的长度是1155,这意味着您缺少一个数字。据推测,缺少的数字在字符串的开头是零,因为&#34; 0x&#34;通常用于指示十六进制数字的符号允许省略前导零。如果这是真的,那么您应该能够使用以下代码进行转换:

// strip off "0x" prefix
string hex = oRequest.image.Substring(2);    

// add leading 0 if length is odd
if (hex.Length % 2 == 1) hex = "0" + hex;    

// convert hex string to bytes
byte[] bytes = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary.Parse(hex).Value;

然后将其添加到SqlCommand,如下所示:

cmd.Parameters.AddWithValue("@P_Doc_Data", bytes);