SQL server插入数据file_get_contents php


sql server insert data file_get_contents php

我有一个PHP代码插入到mysql数据库的数据和blob (img和/或pdf)。

我必须迁移数据库到SQL服务器,当我试图执行代码插入数据到SQL服务器表,SQL服务器显示我这个错误:

 SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]No se permite la conversion implicita del tipo de datos nvarchar(max) a varbinary(max). Utilice la funcion CONVERT para ejecutar esta consulta.

的代码如下:

$destino = addslashes( file_get_contents( $value['tmp_name'] ) );
                $tmpName = $value['tmp_name'];
                // Read the file
                $fp = fopen($tmpName, 'r');
                $data = fread($fp, filesize($tmpName));
                $destino = ($data);
                fclose($fp);
                $tamano = $value['size'];
                $tipo   = $value['type'];
                $name   = $value['name];
$sql="INSERT INTO documentacion 
        ( nombre, archivo,extension ) 
        VALUES 
        ( :nombre, :archivo, :extension)";

$valores = array(
    ':nombre'       =>$name,
    ':archivo'      =>utf8_encode( $destino ),
    ':extension'    =>$tipo,
);
try{
    $ejec=$bd->ejecuta($sql,$valores);  
} 
catch (PDOException $e) {
    echo 'Error BD al insertar documentacion: ' . $e->getMessage();
}
return $ejec[0];

谁能告诉我怎么把nvarchar(max)转换成varbinary(max) ?

谢谢

使用显式转换https://msdn.microsoft.com/en-us/library/ms187928.aspx .

例子
declare @t table (val varbinary(max));
declare @s varchar(max) = 'Some text';
insert @t(val) values(cast(@s as varbinary(max)));
select cast(val as varchar(max))
from @t;

在你的代码中我认为应该是

VALUES 
   ( :nombre, cast(:archivo as varbinary(max)), :extension)