简体   繁体   English

从C#将UTF8数据插入SQL Server 2005中的图像字段

[英]Inserting UTF8 data into image field in sql server 2005 from c#

I have a string made of of xml markup which i encoded using UTF8. 我有一个使用utf8编码的xml标记组成的字符串。 When i insert it into sql server image filed it only inserts the first 28 bytes or characters. 当我将其插入sql server image filed时,它仅插入前28个字节或字符。

Here is my code: 这是我的代码:

  UTF8Encoding utf8 = new UTF8Encoding();
  byte[] encodedBytes = utf8.GetBytes(file.Value);
  string update = String.Format("Update xdpath set content = '{0}' where dpath = '{1}';", encodedBytes, file.Key);
  SqlCommand com = new SqlCommand(update, conn);
  com.ExecuteNonQuery();  

Result in content field of type image : 0x53797374656D2E427974655B5D. 结果在类型为image的内容字段中:0x53797374656D2E427974655B5D。

There is content in the file which is very verbose. 文件中有非常冗长的内容。 Please help. 请帮忙。

Use sql parameters 使用SQL参数

SqlCommand ImageCommand = new SqlCommand("", (SqlConnection)connection, (SqlTransaction)Transaction);

ImageCommand.CommandText = string.Format(@"Update xdpath set content = @ByteArray where dpath = '{0}'",file.Key);

ImageCommand.Parameters.Add("@ByteArray", SqlDbType.Image).Value = (object)encodedBytes ?? DBNull.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM