无法将参数值从 String 转换为 Byte[]
Failed to convert parameter value from a String to a Byte[]
net 代码,当我尝试以二进制格式插入字符串时遇到这个问题。
我在 decal.DecalLogoFileName 中有图像的二进制格式并且 decal.DecalLogoFileName 定义了一个字符串所以当我尝试
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.Binary)).Value=decal.DecalLogoFileName;
decal.DecalLogoFileName="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExIWFhUXGB8aGBcYGR0ZHxobGhoXHRcYGBkaHyghHRolHRcXIjEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGhAQGy0lHyUtLS0tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgABB//EAEgQAAIBAgQDBQQHBQYDCAMAAAECEQADBBIhMQVBUQYTImFxMoGRoSNCUrHB0fAUcpLS4QczU2KCshVUc0Njg5OiwuLxFiRk/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/8QALBEAAgICAgECBQQCAwAAAAAAAAECERIhAzFBE2EiMlFxgUKhsdEEkSMzUv/aAAwDAQACEQMRAD8A"
我在贴花中的字符串值为
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#DecalLogo').attr('src', e.target.result);
$('#hdImageName').val(e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
这里的 hdImageName 值存储在 decal.DecalLogoFileName
我也试过
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.Binary)).Value=Convert.toByte(decal.DecalLogoFileName);
但是转换问题来了。
DecalLogoFileName 的列数据类型设置为 varbinary(MAX)
这样试试;
var byteArray = Encoding.ASCII.GetBytes(decal.DecalLogoFileName);
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.VarBinary, byteArray.Length)).Value=byteArray;
要从数据库中检索图像,您可以使用Encoding.ASCII.GetString(byteArray)
将其转换为base64 字符串。然后你可以像这样把图像放在img src中;
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExIWFhUXGB8aGBcYGR0ZHxobGhoXHRcYGBkaHyghHRolHRcXIjEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGhAQGy0lHyUtLS0tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgABB//EAEgQAAIBAgQDBQQHBQYDCAMAAAECEQADBBIhMQVBUQYTImFxMoGRoSNCUrHB0fAUcpLS4QczU2KCshVUc0Njg5OiwuLxFiRk/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/8QALBEAAgICAgECBQQCAwAAAAAAAAECERIhAzFBE2EiMlFxgUKhsdEEkSMzUv/aAAwDAQACEQMRAD8A"
alt="SampleImage" />
net 代码,当我尝试以二进制格式插入字符串时遇到这个问题。
我在 decal.DecalLogoFileName 中有图像的二进制格式并且 decal.DecalLogoFileName 定义了一个字符串所以当我尝试
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.Binary)).Value=decal.DecalLogoFileName;
decal.DecalLogoFileName="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExIWFhUXGB8aGBcYGR0ZHxobGhoXHRcYGBkaHyghHRolHRcXIjEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGhAQGy0lHyUtLS0tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgABB//EAEgQAAIBAgQDBQQHBQYDCAMAAAECEQADBBIhMQVBUQYTImFxMoGRoSNCUrHB0fAUcpLS4QczU2KCshVUc0Njg5OiwuLxFiRk/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/8QALBEAAgICAgECBQQCAwAAAAAAAAECERIhAzFBE2EiMlFxgUKhsdEEkSMzUv/aAAwDAQACEQMRAD8A"
我在贴花中的字符串值为
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#DecalLogo').attr('src', e.target.result);
$('#hdImageName').val(e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
这里的 hdImageName 值存储在 decal.DecalLogoFileName
我也试过
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.Binary)).Value=Convert.toByte(decal.DecalLogoFileName);
但是转换问题来了。
DecalLogoFileName 的列数据类型设置为 varbinary(MAX)
这样试试;
var byteArray = Encoding.ASCII.GetBytes(decal.DecalLogoFileName);
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.VarBinary, byteArray.Length)).Value=byteArray;
要从数据库中检索图像,您可以使用Encoding.ASCII.GetString(byteArray)
将其转换为base64 字符串。然后你可以像这样把图像放在img src中;
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExIWFhUXGB8aGBcYGR0ZHxobGhoXHRcYGBkaHyghHRolHRcXIjEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGhAQGy0lHyUtLS0tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgABB//EAEgQAAIBAgQDBQQHBQYDCAMAAAECEQADBBIhMQVBUQYTImFxMoGRoSNCUrHB0fAUcpLS4QczU2KCshVUc0Njg5OiwuLxFiRk/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/8QALBEAAgICAgECBQQCAwAAAAAAAAECERIhAzFBE2EiMlFxgUKhsdEEkSMzUv/aAAwDAQACEQMRAD8A"
alt="SampleImage" />