【问题标题】:Inserting UTF8 data into image field in sql server 2005 from c#从c#将UTF8数据插入sql server 2005中的图像字段
【发布时间】:2013-07-11 10:50:05
【问题描述】:

我有一个由我使用 UTF8 编码的 xml 标记组成的字符串。当我将它插入到 sql server 图像文件中时,它只插入前 28 个字节或字符。

这是我的代码:

  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();  

导致图像类型的内容字段:0x53797374656D2E427974655B5D。

文件中有非常冗长的内容。请帮忙。

【问题讨论】:

    标签: c# sql-server utf-8


    【解决方案1】:

    使用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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多