2012-10-22 49 views
0

我正在使用使用ajax控件工具包的默認文本編輯器,並且我需要將用戶可以粘貼到編輯器中的圖像保存到數據庫中。在數據庫中的列VARBINARY(MAX),但是當我嘗試保存我得到以下將圖像從Ajax控件工具包編輯器保存到SQL數據庫

Implicit conversion from data type nvarchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query

我後來轉換的內容錯誤,則在參數定義爲字節像這樣

var subgrantdesc = Convert.ToByte(grantdescription_editor.Content); 

我有這個

var param0 = new SqlParameter(); 
param0.ParameterName = "@desc"; 
param0.SqlDbType = System.Data.SqlDbType.VarBinary; 
param0.Value = subgrantdesc; 
sql.Parameters.Add(param0); 

但是我得到的錯誤:

System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber....

上述錯誤指向我將轉換爲Byte的代碼行。 我想知道如何將通過編輯器控件輸入的數據(圖像和文本)保存到SQL服務器數據庫中。我也想知道HTML格式是否會保留。

任何幫助將不勝感激。

回答

0

應該有兩種方法來解決這個問題。一: 提供字節變量的長度。從here

using(SqlCommand cmd = new SqlCommand("INSERT INTO mssqltable(varbinarycolumn) VALUES (@binaryValue)", conn)) 
{ 
    // Replace 8000, below, with the correct size of the field 
    cmd.Parameters.Add("@binaryValue", SqlDbType.VarBinary, 8000).Value = arraytoinsert; 
    cmd.ExecuteNonQuery(); 
} 

二:使用BitConverter

string data = "0x" + BitConverter.ToString(subgrantdesc).Replace("-", ""); 
相關問題