2013-07-11 67 views
0

我有一個由xml標記組成的字符串,我用UTF8編碼。當我將它插入到sql server映像文件中時,它只會插入前28個字節或字符。從c#插入UTF8數據到sql server 2005中的圖像字段#

這裏是我的代碼:

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。

該文件中有非常詳細的內容。請幫忙。

回答

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; 
+0

Thankssss這麼多。 –

相關問題