2012-10-22 32 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("-", ""); 
相关问题