简体   繁体   中英

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. When i insert it into sql server image filed it only inserts the first 28 bytes or characters.

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.

There is content in the file which is very verbose. Please help.

Use sql parameters

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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