Implicit conversion from varchar to varbinary error when trying to insert an array of bytes into the database

I want to save the file in my database, so I parse it into bytes []:

byte[] f = File.ReadAllBytes(@"E:\Path\To\My\File\MyFile.foo"); 

Class for creating a table:

 public class Files { [AutoIncrement] public int Id { get; set; } public byte[] File { get; set; } } 

Next, in my service, I want to insert my file in db:

 db.Insert(new Files() { File = f }); // db is object to database 

after that I get an error on the site:

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

I do not know how to change this. I tried to save the file by MemoryStream, FileStream, saving bytes per byte to a new byte array, and I always get this error.

Does anyone know how to fix it?

PS I want the files to be saved in the database not only at the URL, so I need to save the byte []. PS2 Insert other types into database operation

thanks for any advice :)

SOLVE:

I solved this:

If you want to put blob in the database, use:

 IDbCommand cmd = db.CreateInsertStatement(new Files() { File = f }); cmd.ExecuteNonQuery(); 

And everything will work perfectly!

Of course, if anyone knows how to put Blob in DB using the "Insert" method - write. I still do not know the correct answer.

+4
source share
1 answer

I had the same problem. Your solution works, but it works more. I sent a request to ServiceStack.OrmLite to fix the problem for SQL Server.

+2
source

Source: https://habr.com/ru/post/1436786/


All Articles