4

I want to save File into my database, so I parse it into byte[]:

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

Class to create 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 into db:

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

after this I get error on site:

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

I have no idea how to change it. I have tried to save File by MemoryStream, FileStream, saving byte per byte into new byte array and I always get this error.

Does anybody know, how to fix it?

P.S. I want ot save files into DB not only URL to files, so I need to save byte[]. P.S.2 Inserting other types into database work

thanks for any advice :)

SOLVED:

I have solved it:

If you want to put blob into database use:

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

And everything will work perfect!

Ofcourse if anyone know how to put Blob to DB with "Insert" method - write it. I still don't know the correct answer.

1
  • If you have answered the question, create an answer and then click "Mark as Answer" :-) Commented Dec 29, 2012 at 19:56

1 Answer 1

2

I experienced the same issue. Your solution works, but it's more of a work around. I submitted a pull request to ServiceStack.OrmLite to fix the problem for SQL Server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.