简体   繁体   English

SQL Server Compact 4.0映像列被截断为8000

[英]SQL Server Compact 4.0 image column truncated to 8000

I am working with SQL Compact 4.0 for the first time and trying to insert data into an IMAGE column and most of the data is around 50kbytes. 我是第一次使用SQL Compact 4.0,并尝试将数据插入到IMAGE列中,并且大多数数据约为50kbytes。

The problem I am getting is although the image column is supposed to be big enough the data is being truncated to 8000 bytes. 我遇到的问题是,尽管image列应该足够大,但数据已被截断为8000个字节。

I am using a SqlCeParameter to insert the data. 我正在使用SqlCeParameter插入数据。 The size has been set to the match the length of the byte[] (43402) and the SqlDbType is SqlDbType.Image. 该大小已设置为与byte []的长度匹配(43402),并且SqlDbType为SqlDbType.Image。

In the database which you can see all the rows are 8000 bytes (using datalength function). 在数据库中,您可以看到所有行均为8000字节(使用datalength函数)。

I suspect this is something in the database schema itself, perhaps a default size set for IMAGE columns which I can hopefully override. 我怀疑这是数据库架构本身的问题,也许是为IMAGE列设置的默认大小,我希望可以覆盖它。 Interesting to note the parameter has the base DbType.Binary - which according to Microsoft is limited to 8000 bytes - could be red herring! 有趣的是,该参数具有基本DbType.Binary(根据Microsoft的规定,该字节数限制为8000个字节)可能是红色鲱鱼!

==== ====

As I couldn't add any images, grrrr. 由于我无法添加任何图像,grrrr。 Here is some more information: 这里是一些更多信息:

Here is the code where I build the command. 这是我构建命令的代码。 _params is just a Dictionary which contains each of the parameter names and data: _params只是一个字典,其中包含每个参数名称和数据:

var cmd = connection.CreateCommand();
cmd.CommandText = _command;

foreach(var p in _params)
{
  var param = SqlHelper.CreateSqlCeParameter(p);
  cmd.Parameters.Add(param);
  if(param.SqlDbType == System.Data.SqlDbType.Image)
  {
     param.Size = (p.Value as byte[]).Length;
  }
}
cmd.ExecuteNonQuery();

Using this helper class to create the parameters: 使用此帮助程序类创建参数:

public class SqlHelper
    {
        public static SqlDbType MapToSqlCeType(object data)
        {
            if(data.GetType() == typeof(string))
            {
                return SqlDbType.NVarChar; 
            }

            if(data.GetType() == typeof(byte[]))
            {
                return SqlDbType.Image;
            }

            return SqlDbType.NVarChar;
        }

        internal static SqlCeParameter CreateSqlCeParameter(KeyValuePair<string, object> data)
        {
            return new SqlCeParameter(data.Key, SqlHelper.MapToSqlCeType(data.Value))
            {
                Value = data.Value
            };
        }
    }

SQL I am using to check: SELECT top 10 datalength([Data]) FROM [Datamodule]; 我正在使用的SQL检查:从[Datamodule]中选择前10个datalength([Data]); GO

Results: Column1 8000 8000 8000 8000 8000 8000 8000 8000 8000 8000 结果:Column1 8000 8000 8000 8000 8000 8000 8000 8000 8000 8000

I figured this out and it was an issue with the way I was creating the SqlCeParameter. 我发现了这一点,这与我创建SqlCeParameter的方式有关。

If I just build the parameter with the name and value then SqlDbType is set to VarBinary (by default) and this inserts all the data into the IMAGE type column. 如果仅使用名称和值构建参数,则SqlDbType设置为VarBinary(默认情况下),这会将所有数据插入IMAGE type列。

So using the following code works fine: 因此,使用以下代码可以正常工作:

 var cmd = connection.CreateCommand();
                    cmd.CommandText = _command;

                    foreach (var p in _params)
                    {
                        cmd.Parameters.Add(new SqlCeParameter(p.Key, p.Value));
                    }

                    cmd.ExecuteNonQuery();

Rather than explicitly setting the SqlDbType to Image, VarBinary appears to do the job. VarBinary似乎没有完成这项工作,而不是将SqlDbType显式设置为Image。 I had previously tried upgrading the database via script and converting/casting the data as VARBINARY had truncated it to 4000 bytes, which I why I went down this route in the first place. 我以前曾尝试通过脚本升级数据库并转换/广播数据,因为VARBINARY已将其截断为4000字节,这就是为什么我首先沿这条路线走的原因。

Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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